#P13761. Chess

Chess

Background

Xiao P likes playing Chinese chess the most. One day he saw an app called “Wanning Chinese Chess”, played a few rounds, and got completely crushed, so he came to you for help.

Problem Description

The board of “Wanning Chinese Chess” has nn rows and mm columns (you can view the board as the first quadrant of a coordinate system). Now a game has reached the endgame. Xiao P has only one elephant (bishop) and one king. The elephant is at the bottom-left corner (1,1)(1,1), and the opponent has only one general at the top-right corner (n,m)(n,m).

It is known that Xiao P moves first. Each turn, he may either move the elephant: it moves in a “field” pattern, cannot leave the board, and is restricted by “blocked elephant eye”; or he may move the king. Assume the king’s movement affects neither the elephant nor the opponent’s general.

Elephant movement rules (those familiar with Chinese chess may skip):

Each time, the elephant moves two squares diagonally, i.e., moving between the bottom-left and top-right corners of a “field”, or between the top-left and bottom-right corners. Formally, if the elephant is at (x,y)(x,y), then it can move next to one of the four squares: (x+2,y+2)(x+2,y+2), (x+2,y2)(x+2,y-2), (x2,y+2)(x-2,y+2), (x2,y2)(x-2,y-2).

“Blocked elephant eye” means there is a piece in the middle of the “field”, i.e., if the intermediate square between the start and end positions contains a piece blocking it, then the elephant cannot move.

The opponent’s general is very dumb and each time will only move in the order down \rightarrow left \rightarrow up \rightarrow right, that is, from (n,m)(n,m) to (n1,m)(n-1,m), then to (n1,m1)(n-1,m-1), then to (n,m1)(n,m-1), and finally back to (n,m)(n,m).

Please determine whether Xiao P’s elephant can capture the opponent’s general. If it can, output the minimum number of moves; otherwise output -1.

Input Format

Only one line containing two positive integers nn and mm, representing that the board has nn rows and mm columns.

Output Format

Output only one number. If the general can be captured, output the minimum number of moves; otherwise output 1-1.

6 5
2
4 4
3
4 5
-1

Hint

[Sample 1 Explanation]

Xiao P’s elephant first goes to (3,3)(3,3). At this time, the opponent’s general moves from (6,5)(6,5) to (5,5)(5,5). On the next move, the elephant can go exactly to (5,5)(5,5) and capture the general, for a total of 22 moves.

[Sample 2 Explanation]

Xiao P first moves the king twice, making the opponent’s general arrive at (3,3)(3,3). Then he moves the elephant to (3,3)(3,3) and captures the general, for a total of 33 moves.

[Constraints]

For 100%100\% of the data, 3n,m10183\leq n,m\leq 10^{18}.

Test Point nn mm
11 3n103 \leq n \leq 10 m=nm=n
22 3m103 \leq m \leq 10
343\sim 4 3n5003 \leq n \leq 500 m=nm=n
565\sim 6 3m5003 \leq m \leq 500
797\sim 9 3n1053 \leq n \leq 10^5 m=nm=n
101210\sim 12 3m1053 \leq m \leq 10^5
131613\sim 16 3n10183 \leq n \leq 10^{18} m=nm=n
172017\sim 20 3m10183 \leq m \leq 10^{18}

Translated by ChatGPT 5