G. The Treasure of the Sierra Jade
time limit per test
1.0 s
memory limit per test
1024 megabytes
input
standard input
output
standard output
Example
Input
5
2 3
1 2
1 2
0 0
0 0
1
3 0 1 1 1 1 1
2 3
1 2
1 2
0 0
0 0
1
2 0 1 1 1
2 3
1 2
1 2
0 0
1 0
0
2 3
1 2
1 2
0 0
1 0
2
1 0 1
1 1 1
3 3
1 2 1
1 1 1
0 2 0
1 0
1
3 0 1 1 1 2 1
Output
3
-1
2
-1
3