Codeforces Round 783 (Div. 2)
A
Direction Change
Codeforces
800
原题
←B →
翻译中…

You are given a grid with $n$ rows and $m$ columns. Rows and columns are numbered from $1$ to $n$, and from $1$ to $m$. The intersection of the $a$-th row and $b$-th column is denoted by $(a, b)$.

翻译中…

Initially, you are standing in the top left corner $(1, 1)$. Your goal is to reach the bottom right corner $(n, m)$.

翻译中…

You can move in four directions from $(a, b)$: up to $(a-1, b)$, down to $(a+1, b)$, left to $(a, b-1)$ or right to $(a, b+1)$.

翻译中…

You cannot move in the same direction in two consecutive moves, and you cannot leave the grid. What is the minimum number of moves to reach $(n, m)$?

翻译中…

Input

翻译中…

The input consists of multiple test cases. The first line contains a single integer $t$ ($1 \le t \le 10^3$) — the number of the test cases. The description of the test cases follows.

翻译中…

The first line of each test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^9$) — the size of the grid.

翻译中…

Output

翻译中…

For each test case, print a single integer: $-1$ if it is impossible to reach $(n, m)$ under the given conditions, otherwise the minimum number of moves.

翻译中…

Note

翻译中…

Test case $1$: $n=1$, $m=1$, and initially you are standing in $(1, 1)$ so $0$ move is required to reach $(n, m) = (1, 1)$.

翻译中…

Test case $2$: you should go down to reach $(2, 1)$.

翻译中…

Test case $3$: it is impossible to reach $(1, 3)$ without moving right two consecutive times, or without leaving the grid.

翻译中…

Test case $4$: an optimal moving sequence could be: $(1, 1) \to (1, 2) \to (2, 2) \to (2, 1) \to (3, 1) \to (3, 2) \to (4, 2)$. It can be proved that this is the optimal solution. So the answer is $6$.

翻译中…

样例

Input #1
6
1 1
2 1
1 3
4 2
4 6
10 5
Output #1
0
1
-1
6
10
17
InputCopy OutputCopy
题目信息
题号
1668A
时间限制
1 s
内存限制
256 MB
标签
模拟
数学

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。