Stanley and Megan decided to shop in the "Crossmarket" grocery store, which can be represented as a matrix with $n$ rows and $m$ columns.

Stanley and Megan can move to an adjacent cell using $1$ unit of power. Two cells are considered adjacent if they share an edge. To speed up the shopping process, Megan brought her portals with her, and she leaves one in each cell she visits (if there is no portal yet). If a person (Stanley or Megan) is in a cell with a portal, that person can use $1$ unit of power to teleport to any other cell with a portal, including Megan's starting cell.

They decided to split up: Stanley will go from the upper-left cell (cell with coordinates $(1, 1)$) to the lower-right cell (cell with coordinates $(n, m)$), whilst Megan needs to get from the lower-left cell (cell with coordinates $(n, 1)$) to the upper-right cell (cell with coordinates $(1, m)$).

What is the minimum total energy needed for them both to do that?

Note that they can choose the time they move. Time does not affect energy.


-----Input-----

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 1000$). Description of the test cases follows.

The only line in the test case contains two integers $n$ and $m$ ($1 \le n, m \le 10^5$).


-----Output-----

For each test case print a single integer on a new line – the answer.


-----Examples-----

Input
7
7 5
5 7
1 1
100000 100000
57 228
1 5
5 1
Output
15
15
0
299998
340
5
5


-----Note-----

In the first test case they can stick to the following plan:

Megan (red circle) moves to the cell $(7, 3)$. Then she goes to the cell $(1, 3)$, and Stanley (blue circle) does the same.

Stanley uses the portal in that cell (cells with portals are grey) to get to the cell $(7, 3)$. Then he moves to his destination — cell $(7, 5)$.

Megan also finishes her route and goes to the cell $(1, 5)$.

The total energy spent is $(2 + 6) + (2 + 1 + 2) + (2)= 15$, which is our final answer.

Source: codeforces | Difficulty: EASY
Tags: Greedy algorithms

Mathematics

Constructive algorithms


Solutions:
t = int(input())
answer_list = []

def function(m, n):
	if n > m:
		a = n
		b = m
		return b - 1 + a + b - 1
	elif n == 1 and m == 1:
		return 0
	else:
		a = m
		b = n
		return b - 1 + a + b - 1
for i in range(t):
	s = input()
	lst = s.split()
	a = int(lst[0])
	b = int(lst[-1])
	answer_list.append(function(a, b))
print(*answer_list, sep='\n')


import sys
input = sys.stdin.readline
for _ in range(int(input())):
	(r, c) = map(int, input().split())
	meg = r - 1 + c - 1
	stan = min([1 + c - 1, 1 + r - 1, r - 1 + c - 1])
	cost = meg + stan
	print(cost)


for s in [*open(0)][1:]:
	(n, m) = sorted(map(int, s.split()))
	print(2 * n + m - 3 + (m > 1))


t = int(input())
res = []
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		res.append(0)
	else:
		res.append(n + m + min(n, m) - 2)
for i in res:
	print(i)


for _ in range(int(input())):
	(n, m) = list(map(int, input().split()))
	(n, m) = [max(n, m), min(n, m)]
	if n == 1 and m == 1:
		print(0)
	elif m == 1:
		print(n)
	else:
		print(n - 1 + m - 1 + m)


t = int(input())
for _ in range(t):
	a = list(map(int, input().split()))
	n = a[0]
	m = a[1]
	if n == 1 and m == 1:
		print(0)
	elif n >= m:
		print(2 * m + n - 2)
	elif n < m:
		print(2 * n + m - 2)


def solve():
	x = int(input())
	for __ in range(x):
		[n, m] = list(map(int, input().split()))
		if n == m == 1:
			print(0)
			continue
		print(n + m - 2 + min(m, n))
solve()


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n == 1 or m == 1:
		print(n + m - 1)
	else:
		print(n + m - 2 + min(n, m))


t = int(input())
for test in range(t):
	www = input().split(' ')
	a = int(www[0])
	b = int(www[1])
	m = min(a, b)
	n = max(a, b)
	k = 2 * m + n - 2
	if m == 1 and n == 1:
		print(0)
	else:
		print(k)


t = int(input())
while t > 0:
	t -= 1
	(a, b) = [int(i) for i in input().split()]
	k = a - 1 + b + b - 1
	k1 = a + a - 1 + b - 1
	if a == 1 and b == 1:
		print(0)
	else:
		print(min(k, k1))


import sys

def solve():
	inp = sys.stdin.readline
	(n, m) = map(int, inp().split())
	if n == 1 and m == 1:
		print(0)
	else:
		print(n - 1 + (m - 1) + min(n, m))

def main():
	for i in range(int(sys.stdin.readline())):
		solve()
main()


import sys
input = sys.stdin.readline
R = lambda : [*map(int, input().split())]
for _ in range(int(input())):
	(n, m) = R()
	if n == 1 and m == 1:
		print(0)
	else:
		print(min(n, m) * 2 + max(n, m) - 2)


import sys
input = iter(sys.stdin.read().splitlines()).__next__

def solve(N, M):
	ret = min((N + M - 2) * 2, N + M - 2 + N - 1 + 1, N + M - 2 + M - 1 + 1)
	return ret
TC = int(input())
for tc in range(TC):
	(N, M) = map(int, input().split())
	res = solve(N, M)
	print(res)


t = int(input())
sol = []
for _ in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		sol.append(0)
		continue
	sol.append(min(m - 1 + n + n - 1, n - 1 + m + m - 1))
for s in sol:
	print(s)


n = int(input())
for e in range(n):
	l = list(map(int, input().split()))
	if l[1] == 1 and l[0] == 1:
		print(0)
	else:
		q = l[0] + l[1] + min(l[0], l[1]) - 2
		print(q)


t = int(input())
for ncase in range(1, t + 1):
	(n, m) = [int(s) for s in input().split(' ')]
	if n == 1 and m == 1:
		ans = 0
	else:
		ans = 2 * min(n, m) + max(n, m) - 2
	print(ans)


case = int(input())
for i in range(case):
	(n, m) = (int(v) for v in input().split())
	if n == m == 1:
		print(0)
	else:
		print(n + m - 2 + min(n, m))


t = int(input())
while t:
	t -= 1
	(n, m) = map(int, input().split())
	if n < 2 and m < 2:
		print(0)
	else:
		(n, m) = (max(n, m), min(n, m))
		print(n + 2 * (m - 1))


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == m == 1:
		print(0)
	else:
		ans = n - 1 + m - 1
		ans += min(n, m)
		print(ans)


k = int(input())
for i in range(k):
	nm = input().split()
	n = max(int(nm[0]), int(nm[1]))
	m = min(int(nm[0]), int(nm[1]))
	if n == 1 and m == 1:
		print(0)
	else:
		print(n + 2 * m - 2)


t = int(input())
for tt in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n == 1:
		print(m)
	elif m == 1:
		print(n)
	else:
		ans = n - 1 + (m - 1) + min(n, m)
		print(ans)


N = int(input())
for _ in range(N):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	else:
		print(n + m + min(n, m) - 2)


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	elif n >= m:
		print(n - 1 + m - 1 + 1 + m - 1)
	else:
		print(n - 1 + m - 1 + 1 + n - 1)


for _ in range(int(input())):
	(a, b) = map(int, input().split())
	z = max(a, b) + 2 * min(a, b) - 2
	if a == 1 and b == 1:
		print(0)
	else:
		print(z)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n < m:
		(n, m) = (m, n)
	if m == 1 and n == 1:
		print(0)
		continue
	print(n + m - 2 + 2 + m - 2)


import sys
t = int(sys.stdin.readline())
for i in range(t):
	(a, b) = map(int, sys.stdin.readline().strip().split())
	lar = max(a, b)
	sma = min(a, b)
	if lar == sma and sma == 1:
		sys.stdout.write(str(0) + '\n')
	else:
		sys.stdout.write(str(lar + (sma - 1) * 2) + '\n')


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	ans = n - 1 + m - 1 + min(n, m) - 1 + 1
	if n == 1 and m == 1:
		print(0)
		continue
	print(ans)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	s = n + m - 2 + min(n, m)
	if n + m != 2:
		print(s)
	else:
		print(0)


from time import sleep
for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split(' ')]
	if n == 1 and m == 1:
		print(0)
	else:
		print(n + m - 1 + n + m - 1 - max(n, m))


t = int(input())
for i in range(t):
	mn = list(map(int, input().split()))
	if mn == [1, 1]:
		print(0)
	else:
		print(max(mn) - 1 + 2 * min(mn) - 1)


t = int(input())
for _ in range(t):
	(n, m) = map(int, input().split())
	if n + m == 2:
		print(0)
	else:
		print(n + m - 2 + min(n, m))


n = int(input())
for _ in range(n):
	(a, b) = map(int, input().split())
	print(min(a + b - 2 + min(a - 1, b - 1) + 1, 2 * (a + b - 2)))


def func(x, y):
	return 2 * x + y - 2
N = int(input())
for i in range(N):
	lens = [int(i) for i in input().split()]
	a = min(lens)
	b = max(lens)
	if a == b == 1:
		print(0)
	else:
		print(func(a, b))


length = int(input())
for _ in range(length):
	(row, col) = list(map(int, input().split(' ')))
	res = row - 1 + (col - 1) + min(row - 1, col - 1)
	if row == 1 and col == 1:
		print(res)
	else:
		print(res + 1)


import sys
input = sys.stdin.readline

def int_num():
	return int(input())

def int_list():
	return list(map(int, input().split()))

def str_list():
	s = input()
	return list(s[:len(s) - 1])

def instr():
	return input().strip()

def invr():
	return map(int, input().split())

def solve():
	(n, m) = int_list()
	if n < m:
		(n, m) = (m, n)
	if n == 1:
		ans = 0
	elif m == 1:
		ans = n
	else:
		ans = n + 2 * (m - 1)
	print(ans)

def main():
	t = int_num()
	while t:
		solve()
		t -= 1
	return
main()


t = int(input())
while t > 0:
	arr = list(map(int, input().split()))
	n = arr[0]
	m = arr[1]
	if n == 1 and m == 1:
		print(0)
	else:
		print(min(2 * m + n - 2, 2 * n + m - 2))
	t -= 1


ts = int(input())
for _ in range(ts):
	(n, m) = [int(i) for i in input().split()]
	if n == 1 and m == 1:
		print(0)
		continue
	ans = min(m, n) + m + n - 2
	print(ans)


kol = int(input())
for j in range(0, kol):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	if n != 1 and m == 1:
		print(n)
	if n == 1 and m != 1:
		print(m)
	if n != 1 and m != 1:
		if n > m:
			print(2 * m + n - 2)
		else:
			print(2 * n + m - 2)


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split(' ')]
	if n == 1 and m == 1:
		print(0)
	else:
		t = n + m - 1
		t *= 2
		print(t - max(n, m))


for i in range(int(input())):
	(a, b) = map(int, input().split())
	(a, b) = (min(a, b), max(a, b))
	if (a, b) == (1, 1):
		print(0)
		continue
	print(a + b - 2 + a)


T = int(input())
for i in range(T):
	(A, B) = map(int, input().split())
	if A + B - min(A, B) - 1 == 0:
		print('0')
	else:
		print(A + B + min(A, B) - 2)


t = int(input())
while t > 0:
	nums = list(map(int, input().split()))
	rows = nums[0]
	columns = nums[1]
	if rows == columns == 1:
		print(0)
	elif rows > columns:
		print(rows + (columns - 1) * 2)
	else:
		print(columns + (rows - 1) * 2)
	t -= 1


for i in range(int(input())):
	(n, m) = map(int, input().split())
	print(n + m - 2 + min(m, n) if max(m, n) > 1 else 0)


for _ in range(int(input())):
	(a, b) = tuple(map(int, input().split()))
	if a == 1 and b == 1:
		print(0)
	else:
		c = min(a, b)
		print(a + b + c - 2)


t = int(input())
answers = []
for i in range(t):
	a = list(map(int, input().split()))
	(a, b) = (a[0], a[1])
	if a == 1 and b == 1:
		answers.append(0)
		continue
	if a > b:
		(a, b) = (b, a)
	answers.append(2 * a + b - 2)
for ans in answers:
	print(ans)


for _ in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if m == n and m == 1:
		print('0')
		continue
	(n, m) = (max(n, m), min(n, m))
	print(n + m - 2 + m)


t = int(input())
for i in range(t):
	(n, m) = input().split()
	n = int(n)
	m = int(m)
	if n == 1 and m == 1:
		print(0)
	else:
		print(n + m - 2 + min(n, m))


import sys
input = sys.stdin.readline
t = int(input())

def solve(n, m):
	if n == 1 and m == 1:
		return 0
	sol = n - 1 + m - 1
	stanley = min(n, m)
	sol += stanley
	return sol
for _ in range(t):
	(n, m) = [int(x) for x in input().split()]
	print(solve(n, m))


t = int(input())
while t:
	(p, q) = map(int, input().split())
	if p == 1 and q == 1:
		print(0)
	elif p == 1:
		print(q)
	elif q == 1:
		print(p)
	else:
		hori = min(p, q)
		hori_len = p + q
		ver = max(p, q)
		print(hori_len + hori - 2)
	t -= 1


n = int(input())
for i in range(n):
	(a, b) = map(int, input().split())
	x = max(a, b)
	y = min(a, b)
	if a == 1 and b == 1:
		print(0)
	else:
		print(x + 2 * y - 2)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	if n == 1 and m == 1:
		print(0)
	else:
		stanley = min(n, m)
		meghan = m - 1 + (n - 1)
		print(stanley + meghan)


p = lambda a: print(a)
iip = lambda : int(input())
sip = lambda : input()
mip = lambda : map(int, input().strip().split())
lip = lambda : list(map(int, input().strip().split()))
ceil = lambda x: int(x) if x == int(x) else int(x) + 1
flr = lambda x: int(x)
tstcs = lambda : range(int(input()))
for t in tstcs():
	(a, b) = mip()
	if a == 1 and b == 1:
		print(0)
		continue
	mm = min(a, b)
	mx = max(a, b)
	print((mm - 1) * 2 + mx)


import math

def solve():
	(n, m) = map(int, input().split(' '))
	if n == m == 1:
		print(0)
		return
	print(max(n, m) + 2 * min(n, m) - 2)
t = int(input())
while t:
	t -= 1
	solve()


def main():
	t = int(input())
	for _ in range(t):
		(x, y) = [int(i) for i in input().split()]
		if x == 1 and y == 1:
			print(0)
		else:
			print(x + y + min(x, y) - 2)
main()


n = int(input())
for i in range(n):
	mat = list(map(int, input().split()))
	if mat[0] == 1 and mat[1] > 1:
		x = mat[1] - 1 + (mat[0] - 1)
		y = 1
	elif mat[0] == 1 and mat[1] == 1:
		x = 0
		y = 0
	elif mat[0] < mat[1]:
		x = mat[1] - 1 + (mat[0] - 1)
		y = mat[0] - 1 + 1
	else:
		x = mat[1] - 1 + (mat[0] - 1)
		y = mat[1] - 1 + 1
	print(x + y)


t = int(input())
for i in range(t):
	(n, m) = map(int, input().split())
	a = max(n, m)
	b = min(n, m)
	if a == b and a == 1:
		print(0)
	else:
		print(a + 2 * (b - 1))


n = int(input())
for i in range(n):
	(a, b) = [int(x) for x in input().split()]
	if a == b == 1:
		print(0)
	elif a > b:
		print(2 * b + a - 2)
	else:
		print(2 * a + b - 2)


ans = []
for testcase in range(int(input())):
	(n, m) = list(map(int, input().split()))
	if n == 1 and m == 1:
		ans.append(0)
	elif n >= m:
		d = m - 2 + n + m
		ans.append(d)
	else:
		d = n + m + n - 2
		ans.append(d)
for i in ans:
	print(i)


n = int(input())
l = []
for i in range(0, n):
	s = [int(x) for x in input().split()]
	l.append(s)
for j in range(0, n):
	n = l[j][0]
	m = l[j][1]
	m1 = min(n, m)
	m2 = max(n, m)
	if m1 == m2 == 1:
		print(0)
	else:
		a = m1 // 2 + (m2 - 1) + (2 * (m1 - 1 - m1 // 2) + 1) + m1 // 2
		print(a)


for _ in range(int(input())):
	(n, m) = map(int, input().split())
	a = max(n, m)
	b = min(n, m)
	if a == 1 and b == 1:
		print(0)
	else:
		res = a + b * 2 - 2
		print(res)


for _ in range(int(input())):
	a = [int(i) for i in input().split()]
	m = a[0]
	n = a[1]
	if m == 1 and n == 1:
		out = 0
	elif m > n:
		out = n - 1 + m - 1 + n - 1 + 1
	else:
		out = m - 1 + n - 1 + m - 1 + 1
	print(out)


for i in range(int(input())):
	(n, m) = [int(x) for x in input().split()]
	if n == 1 and m == 1:
		print(0)
	elif n == m:
		a = (n - 1) * 2 + n
		print(a)
	elif n > m or m > n:
		b = min(n, m)
		c = n - 1 + (m - 1) + b
		print(c)


t = int(input())
for i in range(0, t):
	(n, m) = input().split()
	(n, m) = (int(n), int(m))
	k1 = min(n, m)
	k2 = max(n, m)
	if k1 == 1 and k2 == 1:
		print(0)
	else:
		s = k1 - 2 + k2 + k1
		print(s)


def solution(n, m):
	if n == m and n == 1:
		return 0
	return max(n, m) + 2 * min(n, m) - 2
N = int(input())
for _ in range(N):
	(n, m) = list(map(int, input().split(' ')))
	print(solution(n, m))


t = int(input())
lst1 = [None] * t
for i in range(0, t):
	lst_temp = [int(j) for j in input().split()]
	n = lst_temp[0]
	m = lst_temp[1]
	if n == 1 and m == 1:
		lst1[i] = 0
	else:
		if n < m:
			temp = n
		else:
			temp = m
		lst1[i] = n - 1 + (m - 1) + temp
for i in range(0, t):
	print(lst1[i])


t = int(input())
for i in range(t):
	(a, b) = map(int, input().split())
	if a == 1 and b == 1:
		print('0')
	elif a == 1 and b != 1 or (a != 1 and b == 1):
		print(max(a, b))
	else:
		a = a - 1
		b = b - 1
		print(2 * min(a, b) + max(a, b) + 1)


T = int(input())
for t in range(T):
	(x, y) = map(int, input().split())
	m = min(x, y)
	n = max(x, y)
	c = 0
	c = c + m + n - 2
	c = c + m // 2 + m % 2
	c = c + m - (m // 2 + m % 2)
	if m == 1 and n == 1:
		print('0')
	else:
		print(c)


T = int(input())
ans = []
for tc in range(T):
	(n, m) = map(int, input().split(' '))
	if n == 1 or m == 1:
		if n == 1 and m == 1:
			ans.append(0)
		else:
			ans.append(max(n, m))
	else:
		c = m - 1 + (n - 1) + m
		d = n - 1 + (m - 1) + n
		ans.append(min(c, d))
for i in ans:
	print(i)


tcs = 1 if not True else int(input())
for tc in range(1, tcs + 1):
	(n, m) = map(int, input().split())
	print(0 if n == 1 and m == 1 else 2 * min(m, n) - 2 + max(m, n))


t = int(input())
res = []
for _ in range(t):
	(n, m) = list(map(int, input().split()))
	if n == 1 and m == 1:
		res.append(0)
	elif n > m:
		res.append(n + m + m - 2)
	else:
		res.append(m + n + n - 2)
for r in res:
	print(r)


_ = int(input())
for i in range(_):
	a = [int(i) for i in input().split()]
	if a[0] == 1 and a[1] == 1:
		print(0)
		continue
	else:
		print(a[0] + a[1] + min(a[0], a[1]) - 2)


t = int(input())
l = []
for i in range(0, t):
	m = [int(x) for x in input().split()]
	l.append(m)
for j in range(0, t):
	n = l[j][0]
	m = l[j][1]
	p = max(n, m)
	q = min(n, m)
	if n == m == 1:
		print(0)
	else:
		k = q // 2 + (p - 1) + (2 * (q - 1 - q // 2) + 1) + q // 2
		print(k)


class Solution:

	def crossmarket(self, n, m):
		if n == 1 and m == 1:
			return 0
		return 2 * min(m, n) + max(n, m) - 2
ob = Solution()
t = int(input())
while t:
	(n, m) = list(map(int, input().strip().split()))
	print(ob.crossmarket(n, m))
	t -= 1


t = int(input())
for tt in range(t):
	(x, y) = map(int, input().split())
	n = max(x, y)
	m = min(x, y)
	result = m // 2 + (n - 1) + m + (m - (m // 2 + 1))
	if n == 1 and m == 1:
		print('0')
	else:
		print(result)


test = int(input())
results = []
for i in range(test):
	(n, m) = list(map(int, input().split()))
	if n == 1 and m == 1:
		results.append(0)
		continue
	if n == 1 or m == 1:
		results.append(max(n, m))
		continue
	f = (m - 1) // 2
	d = m - 1 - f
	res1 = 2 * d + n + 2 * f
	f1 = (n - 1) // 2
	d1 = n - 1 - f1
	res2 = 2 * f1 + m + 2 * d1
	results.append(min(res1, res2))
list(map(print, results))


def m09():
	(p, q) = map(int, input().split())
	if p == 1 and q == 1:
		count = 0
		print(count)
	else:
		print(p + q + min(p, q) - 2)
x = int(input())
while x > 0:
	x -= 1
	m09()


for _ in range(0, int(input())):
	(x, y) = map(int, input().split())
	p = min(x, y)
	q = max(x, y)
	if x == 1 and y == 1:
		print(0)
	else:
		print(q + p * 2 - 2)


t = int(input())
for i in range(t):
	c = input()
	c = list(map(int, c.split(' ')))
	n = c[0]
	m = c[1]
	st = [1, 1]
	me = [n, 1]
	if n == m == 1:
		print(0)
	else:
		print(2 * (min(m, n) - 1) + max(m, n))