atcoder#ABC271H. [ABC271Ex] General General
[ABC271Ex] General General
Score : points
Problem Statement
Solve the following problem for test cases.
A piece is placed at the origin on an -plane. You may perform the following operation any number of (possibly zero) times:
- Choose an integer such that and
1
. Let be the current coordinates where the piece is placed.- If , move the piece to .- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
- If , move the piece to .
Your objective is to move the piece to .
Find the minimum number of operations needed to achieve the objective. If it is impossible, print -1
instead.
Constraints
- is
0
or1
. - , , and are integers.
Input
The input is given from Standard Input in the following format:
Here, denotes the -th test case.
Each test case is given in the following format:
Output
Print lines in total. The -th line should contain the answer to the -th test case.
7
5 3 10101010
5 3 01010101
5 3 11111111
5 3 00000000
0 0 11111111
0 1 10001111
-1000000000 1000000000 10010011
8
5
5
-1
0
-1
1000000000