P13761 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.

Description

The board of “Wanning Chinese Chess” has $n$ rows and $m$ 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)$, and the opponent has only one general at the top-right corner $(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)$, then it can move next to one of the four squares: $(x+2,y+2)$, $(x+2,y-2)$, $(x-2,y+2)$, $(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)$ to $(n-1,m)$, then to $(n-1,m-1)$, then to $(n,m-1)$, and finally back to $(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 $n$ and $m$, representing that the board has $n$ rows and $m$ columns.

Output Format

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

Explanation/Hint

**[Sample 1 Explanation]** Xiao P’s elephant first goes to $(3,3)$. At this time, the opponent’s general moves from $(6,5)$ to $(5,5)$. On the next move, the elephant can go exactly to $(5,5)$ and capture the general, for a total of $2$ moves. **[Sample 2 Explanation]** Xiao P first moves the king twice, making the opponent’s general arrive at $(3,3)$. Then he moves the elephant to $(3,3)$ and captures the general, for a total of $3$ moves. **[Constraints]** For $100\%$ of the data, $3\leq n,m\leq 10^{18}$. |Test Point|$n$|$m$| |:-:|:-:|:-:| |$1$|$3 \leq n \leq 10$|$m=n$| |$2$|$3 \leq n \leq 10$|$3 \leq m \leq 10$| |$3\sim 4$|$3 \leq n \leq 500$|$m=n$| |$5\sim 6$|$3 \leq n \leq 500$|$3 \leq m \leq 500$| |$7\sim 9$|$3 \leq n \leq 10^5$|$m=n$| |$10\sim 12$|$3 \leq n \leq 10^5$|$3 \leq m \leq 10^5$| |$13\sim 16$|$3 \leq n \leq 10^{18}$|$m=n$| |$17\sim 20$|$3 \leq n \leq 10^{18}$|$3 \leq m \leq 10^{18}$| Translated by ChatGPT 5