To protect people from evil, 
a long and tall wall was constructed a few years ago. 
But just a wall is not safe, there should also be soldiers on it, 
always keeping vigil. 
The wall is very long and connects the left and the right towers. 
There are exactly N spots (numbered 1 to N) on the wall for soldiers. 
The Kth spot is K miles far from the left tower and (N+1-K) miles from the right tower.

Given a permutation of spots P of {1, 2, ..., N}, soldiers occupy the N spots in that order. 
The P[i]th spot is occupied before the P[i+1]th spot. 
When a soldier occupies a spot, he is connected to his nearest soldier already placed to his left. 
If there is no soldier to his left, he is connected to the left tower. The same is the case with right side. 
A connection between two spots requires a wire of length equal to the distance between the two.

The realm has already purchased a wire of M miles long from Nokia, 
possibly the wire will be cut into smaller length wires. 
As we can observe, the total length of the used wire depends on the permutation of the spots P. Help the realm in minimizing the length of the unused wire. If there is not enough wire, output -1.

-----Input-----
First line contains an integer T (number of test cases, 1 ≤ T ≤ 10 ). Each of the next T lines contains two integers N M, as explained in the problem statement (1 ≤ N ≤ 30 , 1 ≤ M ≤ 1000).

-----Output-----
For each test case, output the minimum length of the unused wire, or -1 if the the wire is not sufficient.

-----Example-----
Input:
4
3 8
3 9
2 4
5 25

Output:
0
0
-1
5

Explanation:

In the 1st case, for example, the permutation P = {2, 1, 3} will use the exact 8 miles wires in total.

In the 2nd case, for example, the permutation P = {1, 3, 2} will use the exact 9 miles wires in total.

To understand the first two cases, you can see the following figures:



In the 3rd case, the minimum length of wire required is 5, for any of the permutations {1,2} or {2,1}, so length 4 is not sufficient.

In the 4th case, for the permutation {1, 2, 3, 4, 5} we need the maximum length of the wire = 20. So minimum possible unused wire length = 25 - 20 = 5.

Source: codechef | Difficulty: UNKNOWN_DIFFICULTY


Solutions:
D = [0] * 31
D[1] = 2
D[2] = 5
for i in range(3, 31):
	best = 10 ** 10
	for p in range(1, i + 1):
		best = min(best, D[p - 1] + D[i - p] + i + 1)
	D[i] = best
t = int(input())
for i in range(t):
	(n, m) = list(map(int, input().split()))
	maxi = (n + 2) * (n + 1) / 2 - 1
	mini = D[n]
	if mini <= m <= maxi:
		print(0)
	elif m < mini:
		print(-1)
	else:
		print(m - maxi)


import sys
T = int(sys.stdin.readline())

def calc_min_len(l, r):
	if l + 1 == r:
		return 0
	if l >= r:
		return 0
	m = (l + r) // 2
	return m - l + r - m + calc_min_len(l, m) + calc_min_len(m, r)

def calc_max_len(n):
	return sum(range(2, N + 2))
for t in range(T):
	(N, M) = list(map(int, sys.stdin.readline().split()))
	min_len = calc_min_len(1, N + 2)
	max_len = calc_max_len(N)
	if M < min_len:
		print(-1)
	elif M > max_len:
		print(M - max_len)
	else:
		print(0)


NumOfTests = int(input())
pwrOfTwo = [2, 4, 8, 16]
for test in range(NumOfTests):
	(N, M) = input().split()
	N = int(N)
	M = int(M)
	if N == 1:
		used_m = 2
		m_div = M - used_m
		if m_div < 0:
			print(-1)
		else:
			print(m_div)
	else:
		(x, y) = divmod(N, 2)
		max_p = x * (N + 3) + y * (N + 3) / 2
		max_pwr = max([x for x in pwrOfTwo if N + 1 >= x])
		min_p = (N + 1) * (pwrOfTwo.index(max_pwr) + 1) + 2 * (N + 1 - max_pwr)
		if min_p > M:
			print(-1)
		elif M <= max_p:
			print(0)
		else:
			print(M - max_p)


import sys
first = True
memmin = {}
memmax = {}

def fmax(n):
	if n == 1:
		return 2
	if n == 0:
		return 0
	if n in memmax:
		return memmax[n]
	res = 0
	for i in range(n):
		cur = i + 1 + n - i + fmax(i) + fmax(n - i - 1)
		if cur > res:
			res = cur
	memmax[n] = res
	return res

def fmin(n):
	if n == 1:
		return 2
	if n == 0:
		return 0
	if n in memmin:
		return memmin[n]
	res = 10 ** 9
	for i in range(n):
		cur = i + 1 + n - i + fmin(i) + fmin(n - i - 1)
		if cur < res:
			res = cur
	memmin[n] = res
	return res
for line in sys.stdin:
	if first:
		first = False
		tc = int(line)
		continue
	tc -= 1
	if tc < 0:
		break
	(n, m) = list(map(int, line.split()))
	val1 = fmin(n)
	val2 = fmax(n)
	if m < val1:
		print(-1)
	elif m > val2:
		print(m - val2)
	else:
		print(0)