题库练习 Direction Change
← 上一题 下一题 →

A15024 | Direction Change

时间限制1s
内存限制256MB
通过 / 提交0/0

题目描述

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)$ ?

输入格式

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.

输出格式

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.

输入输出样例

输入 #1
6
1 1
2 1
1 3
4 2
4 6
10 5
输出 #1
0
1
-1
6
10
17
C++ 编辑器
输入
输出