There are N piles of stones. The i-th pile has A_i stones.
Aoki and Takahashi are about to use them to play the following game:
 - Starting with Aoki, the two players alternately do the following operation:
 - Operation: Choose one pile of stones, and remove one or more stones from it.
 - When a player is unable to do the operation, he loses, and the other player wins.
When the two players play optimally, there are two possibilities in this game: the player who moves first always wins, or the player who moves second always wins, only depending on the initial number of stones in each pile.
In such a situation, Takahashi, the second player to act, is trying to guarantee his win by moving at least zero and at most (A_1 - 1) stones from the 1-st pile to the 2-nd pile before the game begins.
If this is possible, print the minimum number of stones to move to guarantee his victory; otherwise, print -1 instead.

-----Constraints-----
 - 2 \leq N \leq 300
 - 1 \leq A_i \leq 10^{12}

-----Input-----
Input is given from Standard Input in the following format:
N
A_1 \ldots A_N

-----Output-----
Print the minimum number of stones to move to guarantee Takahashi's win; otherwise, print -1 instead.

-----Sample Input-----
2
5 3

-----Sample Output-----
1

Without moving stones, if Aoki first removes 2 stones from the 1-st pile, Takahashi cannot win in any way.
If Takahashi moves 1 stone from the 1-st pile to the 2-nd before the game begins so that both piles have 4 stones, Takahashi can always win by properly choosing his actions.

Difficulty: MEDIUM_HARD

Solutions:
import sys
read = sys.stdin.read
readline = sys.stdin.readline
(n, *a) = map(int, read().split())
v = 0
for i in a[2:]:
	v ^= i
w = a[0] + a[1]
z = w - v
if z & 1 == 1 or z < 0:
	print(-1)
else:
	z //= 2
	if z & v:
		print(-1)
		exit()
	ans = z
	for i in range(60, -1, -1):
		m = 1 << i
		if v & m:
			if ans + m <= a[0]:
				ans += m
	if ans > a[0] or ans == 0:
		print(-1)
	else:
		print(a[0] - ans)


def main():
	import sys

	def input():
		return sys.stdin.readline().rstrip()
	n = int(input())
	a = list(map(int, input().split()))
	x = 0
	for i in range(2, n):
		x ^= a[i]
	d = a[0] + a[1] - x
	if d % 2 == 1 or d < 0:
		print(-1)
		return
	d >>= 1
	if d & x != 0 or d > a[0]:
		print(-1)
		return
	k = x.bit_length()
	tmp = d
	for i in range(k, -1, -1):
		if x >> i & 1:
			if tmp + (1 << i) <= a[0]:
				tmp += 1 << i
	if 0 < tmp <= a[0]:
		print(a[0] - tmp)
	else:
		print(-1)
main()


def examA():
	N = I()
	ans = N + N ** 2 + N ** 3
	print(ans)
	return

def examB():
	S = SI()
	T = SI()
	ans = 0
	for (s, t) in zip(S, T):
		if s != t:
			ans += 1
	print(ans)
	return

def examC():
	(N, M, K) = LI()
	A = LI()
	B = LI()
	SA = 0
	b = 0
	SB = 0
	for i in range(M):
		if SB + B[i] > K:
			break
		SB += B[i]
		b += 1
	ans = 0
	ans = max(ans, b)
	for i in range(N):
		SA += A[i]
		if SA + SB > K:
			while b >= 0:
				if SA + SB <= K:
					break
				SB -= B[b - 1]
				b -= 1
		if b == -1:
			break
		ans = max(ans, b + i + 1)
	print(ans)
	return

def examD():

	def primes(n):
		is_prime = [True] * (n + 1)
		is_prime[0] = False
		is_prime[1] = False
		for i in range(2, int(n ** 0.5) + 1):
			if not is_prime[i]:
				continue
			for j in range(i * 2, n + 1, i):
				is_prime[j] = False
		return [i for i in range(n + 1) if is_prime[i]]
	N = I()
	P = primes(N)
	D = [1] * (N + 1)
	for p in P:
		for i in range(1, 1 + N // p):
			cnt = 1
			cur = i
			while cur % p == 0:
				cnt += 1
				cur //= p
			D[p * i] *= 1 + cnt
	ans = 0
	for i in range(1, N + 1):
		ans += i * D[i]
	print(ans)
	return

def examE():

	class combination:

		def __init__(self, n, mod):
			self.n = n
			self.mod = mod
			self.fac = [1] * (n + 1)
			self.inv = [1] * (n + 1)
			for j in range(1, n + 1):
				self.fac[j] = self.fac[j - 1] * j % mod
			self.inv[n] = pow(self.fac[n], mod - 2, mod)
			for j in range(n - 1, -1, -1):
				self.inv[j] = self.inv[j + 1] * (j + 1) % mod

		def comb(self, n, r):
			if r > n or n < 0 or r < 0:
				return 0
			return self.fac[n] * self.inv[n - r] * self.inv[r] % self.mod
	(N, M) = LI()
	dp = [0] * (N + 1)
	dp[2] = 1
	for n in range(3, N + 1):
		dp[n] += (dp[n - 1] + dp[n - 2]) * (n - 1)
		dp[n] %= mod
	C = combination(M, mod)
	Acur = C.fac[M]
	Acur %= mod
	for i in range(1, N + 1):
		cur = C.comb(N, i) * C.fac[M - i] * C.inv[M - N]
		cur %= mod
		if i % 2 == 1:
			Acur = Acur + mod - cur
		else:
			Acur = Acur + mod + cur
		Acur %= mod
	if N % 2 == 1:
		Acur += 1
	ans = dp[N] * (C.fac[M] * C.inv[M - N])
	ans %= mod
	print(ans)
	return

def examE2():

	class combination:

		def __init__(self, n, mod):
			self.n = n
			self.mod = mod
			self.fac = [1] * (n + 1)
			self.inv = [1] * (n + 1)
			for j in range(1, n + 1):
				self.fac[j] = self.fac[j - 1] * j % mod
			self.inv[n] = pow(self.fac[n], mod - 2, mod)
			for j in range(n - 1, -1, -1):
				self.inv[j] = self.inv[j + 1] * (j + 1) % mod

		def comb(self, n, r):
			if r > n or n < 0 or r < 0:
				return 0
			return self.fac[n] * self.inv[n - r] * self.inv[r] % self.mod
	(N, M) = LI()
	C = combination(M, mod)
	cnt = 0
	for i in range(N + 1):
		cur = C.comb(N, i) * C.fac[M - i] * C.inv[M - N] % mod
		if i % 2 == 1:
			cnt -= cur
			cnt += mod
			cnt %= mod
		else:
			cnt += cur
			cnt %= mod
	ans = cnt * (C.fac[M] * C.inv[M - N])
	ans %= mod
	print(ans)
	return

def examF():
	bit = 40
	N = I()
	A = LI()
	aim = 0
	start = A[0] + A[1]
	for a in A[2:]:
		aim ^= a
	cnt = 0
	stack = deque()
	candi = []
	for i in range(bit):
		if 1 << i & aim:
			cnt += 1 << i
			candi.append(1 << i)
		else:
			stack.append(1 << i)
	a = 0
	b = 0
	while stack:
		now = stack.pop()
		if cnt + now * 2 <= start:
			cnt += now * 2
			a += now
			b += now
		if cnt == start:
			break
	if start != cnt:
		print(-1)
		return
	if a > A[0]:
		print(-1)
		return
	for c in candi[::-1]:
		if a + c <= A[0]:
			a += c
	ans = A[0] - a
	if ans == A[0]:
		print(-1)
		return
	print(ans)
	return
from decimal import getcontext, Decimal as dec
import sys, bisect, itertools, heapq, math, random
from copy import deepcopy
from heapq import heappop, heappush, heapify
from collections import Counter, defaultdict, deque
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

def I():
	return int(input())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def DI():
	return dec(input())

def LDI():
	return list(map(dec, sys.stdin.readline().split()))

def LSI():
	return list(map(str, sys.stdin.readline().split()))

def LS():
	return sys.stdin.readline().split()

def SI():
	return sys.stdin.readline().strip()
global mod, mod2, inf, alphabet, _ep
mod = 10 ** 9 + 7
mod2 = 998244353
inf = 10 ** 18
_ep = dec('0.000000000001')
alphabet = [chr(ord('a') + i) for i in range(26)]
alphabet_convert = {chr(ord('a') + i): i for i in range(26)}
getcontext().prec = 28
sys.setrecursionlimit(10 ** 7)
examF()


import sys

def solve():
	input = sys.stdin.readline
	N = int(input())
	A = [int(a) for a in input().split()]
	xorR = 0
	for i in range(2, N):
		xorR ^= A[i]
	rbit = [0] * 41
	K = xorR
	for i in range(41):
		rbit[i] = K % 2
		K //= 2
	D = A[0] + A[1] - xorR
	if D < 0 or D % 2 == 1:
		print(-1)
		return 0
	else:
		D //= 2
		if D > A[0]:
			print(-1)
			return 0
		K = D
		dbit = [0] * 41
		for i in range(41):
			dbit[i] = K % 2
			K //= 2
		A0 = D
		for i in range(41):
			if rbit[40 - i] == dbit[40 - i] and rbit[40 - i] == 1:
				print(-1)
				return 0
			elif rbit[40 - i] == 1:
				if A0 + pow(2, 40 - i) <= A[0]:
					A0 += pow(2, 40 - i)
		if A0 > 0:
			print(A[0] - A0)
		else:
			print(-1)
	return 0

def __starting_point():
	solve()
__starting_point()


def resolve():
	N = int(input())
	A = list(map(int, input().split()))
	S = A[0] + A[1]
	xor = 0
	for a in A[2:]:
		xor ^= a
	S -= xor
	if S % 2 == 1:
		return print(-1)
	S //= 2
	if S & xor != 0 or S > A[0]:
		return print(-1)
	b = S
	for i in range(41, -1, -1):
		if xor >> i & 1 == 1:
			if b + (1 << i) <= A[0]:
				b += 1 << i
	if b == 0:
		print(-1)
	else:
		print(A[0] - b)

def __starting_point():
	resolve()
__starting_point()


from functools import reduce
from collections import defaultdict
N = int(input())
A = list(map(int, input().split()))
r = reduce(lambda x, y: x ^ y, A[2:], 0)
(a, b) = A[:2]

def answer(ta, tb, tr):
	for i in range(ta):
		if ta - i ^ tb + i == tr:
			ans = i
			break
	else:
		ans = -1
	return ans

def solve(a, b, r):
	orga = a
	ans = 0
	l = max(map(lambda x: len(bin(x)), [a, b, r])) - 2
	cand = {(a, b, r): 0}
	for i in range(l):
		temp = defaultdict(lambda : 10 ** 20)
		for ((a, b, r), s) in cand.items():
			(x, y, z) = (a % 2, b % 2, r % 2)
			if not x ^ y == z:
				continue
			else:
				if s < temp[a // 2, b // 2, r // 2]:
					temp[a // 2, b // 2, r // 2] = s
				a -= 1
				b += 1
				if a < 0:
					continue
				s += 1 << i
				if s < temp[a // 2, b // 2, r // 2]:
					temp[a // 2, b // 2, r // 2] = s
		cand = temp
	if not (0, 0, 0) in cand:
		ans = -1
	else:
		ans = cand[0, 0, 0]
		if ans == orga:
			ans = -1
	return ans
print(solve(a, b, r))


n = int(input())
a = list(map(int, input().split()))
al = 0
for i in range(2, n):
	al ^= a[i]
ans = 0
s = a[0] + a[1]
if s - al < 0 or (s - al) % 2:
	ans = -1
else:
	d = (s - al) // 2
	now = d
	for i in range(65, -1, -1):
		if 1 << i & al and (1 << i) + now <= a[0]:
			now += 1 << i
	if now == 0 or now > a[0] or d & al != 0:
		ans = -1
	else:
		ans = a[0] - now
print(ans)


import sys
N = int(input())
A = list(map(int, input().split()))
x = 0
for i in A:
	x ^= i
old = A[0] ^ A[1] ^ x
b0pb1 = A[0] + A[1]
if b0pb1 < old or (b0pb1 - old) % 2 == 1:
	print(-1)
	exit(0)
b0andb1 = (b0pb1 - old) // 2
print(A[0], A[1], b0pb1, old, b0andb1, file=sys.stderr)
if b0andb1 & old != 0:
	print(-1)
	exit(0)
if A[0] < b0andb1:
	print(-1)
	exit(0)
ans = b0andb1
for i in reversed(range(0, 40)):
	if old >> i & 1 == 0:
		continue
	if ans + (1 << i) <= A[0]:
		ans += 1 << i
if ans == 0:
	print(-1)
	exit(0)
print(A[0] - ans)


import sys
n = int(input())
a = list(map(int, input().split()))
c = a[0] + a[1]
t = 0
for ai in a[2:]:
	t = t ^ ai
bit_len = c.bit_length() + 1
if c < t:
	print(-1)
	sys.exit()
if (c - t) % 2:
	print(-1)
	sys.exit()
b1 = (c - t) // 2
a1 = b1
a2 = b1
if a1 > a[0]:
	print(-1)
	sys.exit()
for i in reversed(range(bit_len)):
	if t & 1 << i:
		if b1 & 1 << i:
			print(-1)
			sys.exit()
		if a1 + 2 ** i <= a[0]:
			a1 += 2 ** i
if a1 == 0:
	print(-1)
else:
	print(a[0] - a1)


def resolve():
	N = int(input())
	A = [int(a) for a in input().split()]
	x = 0
	for a in A[2:]:
		x ^= a
	s = sum(A[:2])
	if s < x or (s ^ x) & 1:
		print(-1)
		return
	d = (s - x) // 2
	a = A[0]
	n = 0
	for i in range(40)[::-1]:
		(b, c) = (x >> i & 1, d >> i & 1)
		if b == c == 1:
			print(-1)
			return
		if b == 0 and c == 1:
			n += 1 << i
	for i in range(40)[::-1]:
		(b, c) = (x >> i & 1, d >> i & 1)
		if b == 1 and c == 0:
			if n + (1 << i) <= a:
				n += 1 << i
	if n > a or n == 0:
		print(-1)
	else:
		print(a - n)
resolve()


import math
import queue
from collections import deque, defaultdict
import heapq as hpq
from sys import stdin, setrecursionlimit
ipt = stdin.readline
setrecursionlimit(10 ** 7)

def main():
	n = int(ipt())
	a = [int(i) for i in ipt().split()]
	x = 0
	for i in a[2:]:
		x ^= i
	s = a[0] + a[1]
	d = (s - x) // 2
	if d < 0 or d & x or s - x & 1 or (d > a[0]):
		print(-1)
		exit()
	a1 = d
	xi = 1
	kx = x
	while kx > 0:
		xi *= 2
		kx >>= 1
	xi //= 2
	while xi > 0:
		if x & xi:
			if a1 + xi <= a[0]:
				a1 += xi
			x -= xi
		xi //= 2
	if a1 == 0:
		print(-1)
		exit()
	else:
		print(a[0] - a1)
	return
main()


import sys
(N,) = map(int, input().split())
X = list(map(int, input().split()))
S = X[0] + X[1]
T = 0
for i in range(2, N):
	T ^= X[i]
if S < T or (S - T) % 2:
	print(-1)
	sys.exit()
D = (S - T) // 2
if D & T:
	print(-1)
	sys.exit()
Y = D
for i in range(T.bit_length() + 1, -1, -1):
	if T & 1 << i | Y <= X[0]:
		Y |= T & 1 << i
if 0 < Y <= X[0]:
	print(X[0] - Y)
else:
	print(-1)


import sys
input = sys.stdin.buffer.readline
sys.setrecursionlimit(10 ** 7)
from collections import defaultdict
con = 10 ** 9 + 7
INF = float('inf')

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

def main():
	N = int(input())
	A = getlist()
	XOR = 0
	for i in range(2, N):
		XOR ^= A[i]
	Asum = A[0] + A[1]
	ans = INF
	AA = Asum
	a0 = 0
	a1 = 0
	for i in range(50, -1, -1):
		if 2 ** i & XOR == 2 ** i:
			AA -= 2 ** i
	for i in range(50, -1, -1):
		if 2 ** i & XOR == 0:
			if AA >= 2 ** (i + 1):
				AA -= 2 ** (i + 1)
				a0 += 2 ** i
				a1 += 2 ** i
			else:
				pass
	for i in range(50, -1, -1):
		if 2 ** i & XOR == 2 ** i:
			if a0 + 2 ** i <= A[0]:
				a0 += 2 ** i
			else:
				a1 += 2 ** i
	if a0 == 0 or a0 > A[0] or a0 + a1 != Asum or (a0 ^ a1 != XOR):
		print(-1)
	else:
		print(A[0] - a0)
main()


def f_unfair_nim():
	from functools import reduce
	from operator import xor
	N = int(input())
	A = [int(i) for i in input().split()]
	s = sum(A[:2])
	x = reduce(xor, A[2:], 0)
	if (s - x) % 2 != 0 or (s - x) // 2 & x != 0:
		return -1
	a = (s - x) // 2
	for i in range(60, -1, -1):
		if x & 1 << i != 0 and a ^ 1 << i <= A[0]:
			a ^= 1 << i
	return A[0] - a if A[0] >= a > 0 else -1
print(f_unfair_nim())


from collections import defaultdict, deque, Counter
from heapq import heappush, heappop, heapify
import bisect
import random
from itertools import permutations, accumulate, combinations, product
import sys
import string
from bisect import bisect_left, bisect_right
from math import factorial, ceil, floor, gcd, sqrt
from operator import mul
from functools import reduce
from operator import mul
from pprint import pprint
sys.setrecursionlimit(2147483647)
INF = 10 ** 20

def LI():
	return list(map(int, sys.stdin.buffer.readline().split()))

def I():
	return int(sys.stdin.buffer.readline())

def LS():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8').split()

def S():
	return sys.stdin.buffer.readline().rstrip().decode('utf-8')

def IR(n):
	return [I() for i in range(n)]

def LIR(n):
	return [LI() for i in range(n)]

def SR(n):
	return [S() for i in range(n)]

def LSR(n):
	return [LS() for i in range(n)]

def SRL(n):
	return [list(S()) for i in range(n)]

def MSRL(n):
	return [[int(j) for j in list(S())] for i in range(n)]
mod = 1000000007
n = int(input())
a = list(map(int, input().split()))
t = 0
flg = 0
for ai in a[2:]:
	t = t ^ ai
b1 = a[0] + a[1] - t
if b1 < 0 or b1 % 2:
	flg = 1
b1 //= 2
a1 = b1
if a1 > a[0]:
	flg = 1
for i in reversed(range((a[0] + a[1]).bit_length())):
	if t & 1 << i:
		if b1 & 1 << i:
			flg = 1
		if a1 + 2 ** i <= a[0]:
			a1 += 2 ** i
if a1 == 0:
	flg = 1
print(a[0] - a1 if flg == 0 else -1)


N = int(input())
A = [int(a) for a in input().split()]
t = 0
for a in A[2:]:
	t ^= a
s = sum(A[:2])
if s < t or (s ^ t) & 1:
	print(-1)
	return
u = (s - t) // 2
a = A[0]
n = 0
for i in range(40)[::-1]:
	(b, c) = (t >> i & 1, u >> i & 1)
	if b == c == 1:
		print(-1)
		return
	if b == 0 and c == 1:
		n += 1 << i
for i in range(40)[::-1]:
	(b, c) = (t >> i & 1, u >> i & 1)
	if b == 1 and c == 0:
		if n + (1 << i) <= a:
			n += 1 << i
if n > a or n == 0:
	print(-1)
else:
	print(a - n)


def solve(ls):
	import math
	(x, y, *ls) = ls
	z = x + y
	s = 0
	for a in ls:
		s ^= a
	if z - s < 0 or (z - s) % 2 == 1:
		return -1
	t = (z - s) // 2
	s_t = s & t
	if s_t > 0 or t > x:
		return -1
	a = t
	b = t
	first_set = math.floor(math.log2(s + 1))
	for i in range(first_set + 1)[::-1]:
		k = 1 << i
		if s & k:
			if a | k <= x:
				a = a | k
			else:
				b = b | k
	if a == 0:
		return -1
	return x - a

def main(istr, ostr):
	n = int(istr.readline().strip())
	ls = list(map(int, istr.readline().strip().split()))
	result = solve(ls)
	print(result, file=ostr)

def __starting_point():
	import sys
	main(sys.stdin, sys.stdout)
__starting_point()


n = int(input())
l = list(map(int, input().split()))
x = 0
s = l[0] + l[1]
for i in l[2:]:
	x ^= i
if x > s or (s - x) % 2 or (s - x) // 2 & x:
	print(-1)
	exit()
a = (s - x) // 2
if a > l[0]:
	print(-1)
	exit()
for i in range(50, -1, -1):
	if 1 << i & x:
		if a + (1 << i) <= l[0]:
			a += 1 << i
if a == 0:
	print(-1)
else:
	print(l[0] - a)


def digit(x):
	ret = 0
	while x > 0:
		x //= 2
		ret += 1
	return ret

def main():
	N = int(input())
	A = list(map(int, input().split()))
	g = 0
	for i in range(2, N):
		g ^= A[i]
	a0 = A[0]
	a1 = A[1]
	ans = 0
	half = 2 ** 20
	if a0 < half:
		for i in range(a0):
			t0 = a0 - i
			t1 = a1 + i
			if t0 ^ t1 == g:
				print(i)
				return
		print(-1)
		return
	ug = g >> 20
	b0 = a0 >> 20
	b1 = a1 >> 20
	upper_ans_0 = -1
	for i in range(b0):
		t0 = b0 - i
		t1 = b1 + i
		if t0 ^ t1 == ug:
			upper_ans_0 = i
			break
	upper_ans_1 = -1
	for i in range(b0):
		t0 = b0 - i
		t1 = b1 + 1 + i
		if t0 ^ t1 == ug:
			upper_ans_1 = i
			break
	upper_ans_2 = -1
	for i in range(b0 - 1):
		t0 = b0 - 1 - i
		t1 = b1 + i
		if t0 ^ t1 == ug:
			upper_ans_2 = i
			break
	upper_ans_3 = -1
	for i in range(b0 - 1):
		t0 = b0 - 1 - i
		t1 = b1 + 1 + i
		if t0 ^ t1 == ug:
			upper_ans_3 = i
			break
	lower_answers = []
	for i in range(half):
		t0 = (a0 - i) % half
		t1 = (a1 + i) % half
		if t0 ^ t1 == g % half:
			lower_answers.append(i)
	if len(lower_answers) == 0:
		print(-1)
		return
	answers = []
	for l in lower_answers:
		if a0 % half >= l:
			if a1 % half + l < half:
				if upper_ans_0 != -1:
					answers.append((upper_ans_0 << 20) + l)
			elif upper_ans_1 != -1:
				answers.append((upper_ans_1 << 20) + l)
		elif a1 % half + l < half:
			if upper_ans_2 != -1:
				answers.append((upper_ans_2 << 20) + l)
		elif upper_ans_3 != -1:
			answers.append((upper_ans_3 << 20) + l)
	if len(answers) == 0:
		print(-1)
		return
	answers.sort()
	print(answers[0])
main()


n = int(input())
a = list(map(int, input().split()))
x = 0
for i in range(2, n):
	x ^= a[i]
s = a[0] + a[1]
if s < x or (s - x) % 2 == 1:
	print(-1)
	exit()
d = (s - x) // 2
if d & x != 0 or d > a[0]:
	print(-1)
	exit()
i = 50
ans = d
while i >= 0:
	if x >> i & 1:
		if (1 << i) + ans <= a[0]:
			ans += 1 << i
	i -= 1
if ans == 0:
	print(-1)
	exit()
print(a[0] - ans)


n = int(input())
A = [int(i) for i in input().split()]
A1 = A[0]
A2 = A[1]
c = 0
for ele in A[2:]:
	c ^= ele
d = A1 + A2 - c
if d % 2 == 1:
	print(-1)
else:
	d = d // 2
	flgs = []
	for i in range(41):
		fand = d & 1 << i
		fxor = c & 1 << i
		if fand & fxor:
			print(-1)
			break
		elif fand:
			flgs.append(1)
		elif fxor:
			flgs.append(2)
		else:
			flgs.append(0)
	else:
		flgs = flgs[::-1]
		ans = 0
		for i in range(len(flgs)):
			if flgs[i] == 1:
				ans += 2 ** (41 - i - 1)
		if ans > A1:
			print(-1)
		else:
			for i in range(len(flgs)):
				if flgs[i] == 2:
					if ans + 2 ** (41 - i - 1) <= A1:
						ans += 2 ** (41 - i - 1)
			if ans == 0:
				print(-1)
			else:
				print(A1 - ans)


import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10 ** 9))
write = lambda x: sys.stdout.write(x + '\n')
n = int(input())
a = list(map(int, input().split()))
val = 0
for num in a[2:]:
	val ^= num
k = a[0] + a[1]
if (k - val) % 2 != 0 or k < val:
	ans = -1
else:
	tmp = (k - val) // 2
	if tmp < 0 or 2 * tmp > k:
		ans = -1
	else:
		v1 = tmp
		v2 = tmp
		kouho = []
		ans = None
		if tmp > a[0]:
			ans = -1
		diff = k - 2 * tmp
		for i in range(diff.bit_length()):
			if diff & 1 << i & tmp:
				ans = -1
				break
			elif diff & 1 << i:
				kouho.append(1 << i)
		s = sum(kouho)
		if ans is None:
			for num in kouho[::-1]:
				s -= num
				if v2 + s < a[1]:
					v2 += num
				elif v1 + num <= a[0]:
					v1 += num
				else:
					v2 += num
			if v1 == 0:
				ans = -1
			else:
				ans = a[0] - v1
		else:
			ans = -1
print(ans)


(N,) = map(int, input().split())
X = list(map(int, input().split()))
t = 0
for x in X:
	t = t ^ x
inf = float('inf')
d = dict()

def it(x, y, z):
	if (x, y, z) in d:
		return d[x, y, z]
	(xm, ym, zm) = (x % 2, y % 2, z % 2)
	if xm ^ ym != zm:
		d[x, y, z] = inf
		return inf
	if x ^ y == z:
		d[x, y, z] = 0
		return 0
	Rz = 2 * it(x >> 1, y >> 1, z >> 1)
	Ro = inf
	if x:
		Ro = 2 * it(x - 1 >> 1, y + 1 >> 1, z >> 1) + 1
	Rz = min(Rz, Ro)
	d[x, y, z] = Rz
	return Rz
r = it(X[0], X[1], t ^ X[0] ^ X[1])
if 0 <= r < X[0]:
	print(r)
else:
	print(-1)


n = int(input())
(a, b, *L) = map(int, input().split())
x = 0
for l in L:
	x ^= l
s = a + b
if s - x < 0 or (s - x) % 2:
	print(-1)
	exit()
d = (s - x) // 2
if d & x:
	print(-1)
	exit()
if d > a:
	print(-1)
	exit()
for i in range(50, -1, -1):
	if x >> i & 1 and d | 1 << i <= a:
		d |= 1 << i
if d == 0:
	print(-1)
	exit()
ans = a - d
print(ans)


n = int(input())
a = list(map(int, input().split()))
S = a[0] + a[1]
C = 0
for i in range(2, n):
	C ^= a[i]
D = S - C
if D < 0 or D % 2 == 1:
	print(-1)
	exit()
D //= 2
ans = 0
free_keta = []
for i in range(100):
	if C & 1 << i != 0:
		free_keta.append(i)
		if D & 1 << i != 0:
			print(-1)
			exit()
	else:
		ans += D & 1 << i
if ans > a[0]:
	print(-1)
	exit()
for keta in free_keta[::-1]:
	if ans + (1 << keta) <= a[0]:
		ans += 1 << keta
if ans == 0:
	print(-1)
	exit()
print(a[0] - ans)


import pprint
N = int(input())
A = list(map(int, input().split()))
bitlen = 42
flag = 1
exor = 0
andsum = 0
ex = [0] * bitlen
an = [0] * bitlen
num = 0
for i in range(2, N):
	exor = exor ^ A[i]
andsum = A[0] + A[1] - exor
if andsum % 2 != 0 or andsum < 0:
	flag = 0
else:
	andsum = andsum // 2
	for i in range(bitlen):
		if exor >> i & 1:
			ex[i] = 1
		if andsum >> i & 1:
			an[i] = 1
	num = andsum
	for i in range(bitlen):
		if an[i] == 1 and ex[i] == 1:
			flag = 0
			break
	if num > A[0]:
		flag = 0
	for i in range(bitlen - 1, -1, -1):
		if ex[i] == 1:
			if num + pow(2, i) <= A[0]:
				num = num + pow(2, i)
	if num == 0:
		flag = 0
if flag == 0:
	print(-1)
else:
	print(A[0] - num)


import random
N = int(input())
A = list(map(int, input().split()))
S = A[0] + A[1]
X = 0
for a in A[2:]:
	X ^= a
D = S - X >> 1
if S != X + (D << 1) or D > A[0] or X & D != 0:
	print(-1)
	return
a = D
b = 2 ** 64
while b > 0:
	if X & b != 0 and a | b <= A[0]:
		a |= b
	b >>= 1
if a == 0:
	print(-1)
	return
print(A[0] - a)


N = int(input())
A = list(map(int, input().split()))
P = 41
X = 0
for i in range(2, N):
	X ^= A[i]
S = A[0] + A[1]
A1 = A[0]
(D, M) = divmod(S - X, 2)
ans = -1
if M % 2 == 0 and D & X == 0 and (D <= A1):
	Y = 0
	for p in range(P, -1, -1):
		if X >> p & 1 == 1:
			new_Y = Y ^ 1 << p
			if D ^ new_Y <= A1:
				Y = new_Y
	ans = A1 - (D ^ Y)
	if ans == A1:
		ans = -1
print(ans)


N = int(input())
A = [int(i) for i in input().split()]
S = A[0] + A[1]
X = 0
for i in range(2, N):
	X ^= A[i]
dp = [[[-1] * 2 for _ in range(2)] for _ in range(60)]
dp[0][0][0] = 0
for i in range(59):
	cs = S >> i & 1
	cx = X >> i & 1
	ca = A[0] >> i & 1
	for j in (0, 1):
		for k in (0, 1):
			if dp[i][j][k] < 0:
				continue
			for na in (0, 1):
				for nb in (0, 1):
					ni = i + 1
					nj = 0
					nk = k
					if na ^ nb != cx:
						continue
					ns = na + nb + j
					if ns % 2 != cs:
						continue
					if ns >= 2:
						nj = 1
					if ca < na:
						nk = 1
					elif ca == na:
						nk = k
					else:
						nk = 0
					dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k] | na << i)
if 1 <= dp[-1][0][0] <= A[0]:
	print(A[0] - dp[-1][0][0])
else:
	print(-1)


N = list(map(int, input().split()))
As = list(map(int, input().split()))
added = As[0] + As[1]
xored = 0
for A in As[2:]:
	xored ^= A
anded = added - xored
if anded % 2 == 1:
	print(-1)
	return
else:
	anded >>= 1
left_stones_l_A0 = ''
left_stones_e_A0 = ''
for (i, (a, x)) in enumerate(zip(format(anded, '045b'), format(xored, '045b'))):
	if x == '0':
		if a == '0':
			left_stones_l_A0 += '0'
			left_stones_e_A0 += '0'
		else:
			left_stones_l_A0 += '1'
			left_stones_e_A0 += '1'
	elif a == '0':
		if int(left_stones_l_A0 + '1' + '1' * (44 - i), base=2) <= As[0]:
			left_stones_l_A0 += '1'
		else:
			left_stones_l_A0 += '0'
		if int(left_stones_e_A0 + '1' + '1' * (44 - i), base=2) <= As[0]:
			left_stones_e_A0 += '1'
			left_stones_l_A0 = left_stones_e_A0
		elif int(left_stones_e_A0 + '1' + '0' * (44 - i), base=2) <= As[0]:
			left_stones_l_A0 = left_stones_e_A0 + '0'
			left_stones_e_A0 += '1'
		elif int(left_stones_e_A0 + '0' + '0' * (44 - i), base=2) <= As[0]:
			left_stones_e_A0 += '0'
		else:
			left_stones_e_A0 = left_stones_l_A0
	else:
		print(-1)
		return
if int(left_stones_e_A0, base=2) <= As[0]:
	ans = As[0] - int(left_stones_e_A0, base=2)
else:
	ans = As[0] - int(left_stones_l_A0, base=2)
if 0 <= ans < As[0]:
	print(ans)
else:
	print(-1)


import sys
n = int(input())
a = [int(x) for x in input().split()]
T = 0
for i in range(2, n):
	T ^= a[i]
S = a[0] + a[1]
D = (S - T) // 2
if S < T or (S - T) % 2 or D & T:
	print(-1)
	sys.exit()
for i in range(T.bit_length() + 1, -1, -1):
	if T & 1 << i | D <= a[0]:
		D |= T & 1 << i
if 0 < D <= a[0]:
	print(a[0] - D)
else:
	print(-1)


import sys
int1 = lambda x: int(x) - 1
p2D = lambda x: print(*x, sep='\n')

def II():
	return int(sys.stdin.readline())

def MI():
	return map(int, sys.stdin.readline().split())

def LI():
	return list(map(int, sys.stdin.readline().split()))

def LLI(rows_number):
	return [LI() for _ in range(rows_number)]

def SI():
	return sys.stdin.readline()[:-1]

def main():
	n = II()
	aa = LI()
	x = 0
	for a in aa[2:]:
		x ^= a
	t = aa[0] + aa[1] - x
	if t < 0 or t & 1:
		print(-1)
		return
	t >>= 1
	a0 = a1 = 0
	for i in range(40, -1, -1):
		dt = t >> i & 1
		dx = x >> i & 1
		if (dt, dx) == (1, 1):
			print(-1)
			return
		if (dt, dx) == (1, 0):
			a0 |= 1 << i
			a1 |= 1 << i
	for i in range(40, -1, -1):
		dt = t >> i & 1
		dx = x >> i & 1
		if (dt, dx) == (0, 1):
			if a0 | 1 << i <= aa[0]:
				a0 |= 1 << i
			else:
				a1 |= 1 << i
	if a0 > 0 and a0 <= aa[0]:
		print(aa[0] - a0)
	else:
		print(-1)
main()


import sys
input = sys.stdin.readline
N = int(input())
a = list(map(int, input().split()))
xsm = 0
sm = a[0] + a[1]
for i in range(2, N):
	xsm ^= a[i]
t = sm - xsm
table = [0] * 41
for i in range(40, -1, -1):
	if t >= 1 << i + 1:
		t -= 1 << i + 1
		table[i] = 1
if t:
	print(-1)
	exit(0)
t = a[0]
res = 0
for i in range(40, -1, -1):
	if t >= 1 << i:
		if table[i]:
			t -= 1 << i
			res += 1 << i
for i in range(40, -1, -1):
	if t >= 1 << i:
		if xsm & 1 << i:
			t -= 1 << i
			res += 1 << i
if res == 0 or res ^ sm - res != xsm:
	print(-1)
else:
	print(a[0] - res)


import sys
from functools import lru_cache
sys.setrecursionlimit(10 ** 9)
INF = 10 ** 18
MOD = 10 ** 9 + 7
input = lambda : sys.stdin.readline().rstrip()
YesNo = lambda b: bool([print('Yes')] if b else print('No'))
YESNO = lambda b: bool([print('YES')] if b else print('NO'))
int1 = lambda x: int(x) - 1
N = input()
A = tuple(map(int, input().split()))
X = 0
for x in A[2:]:
	X ^= x
S = A[0] + A[1]
if (S - X) % 2:
	print(-1)
	exit()
D = (S - X) // 2
ans = 0
tmp = 0
ttmp = 0
f = False
for i in range(50):
	if D >> i & 1 == X >> i & 1 == 1:
		print(-1)
		exit()
	elif D >> i & 1 == 1 and X >> i & 1 == 0:
		if A[0] >> i & 1 == 0:
			ans -= 1 << i
			f = True
	elif D >> i & 1 == 0 and X >> i & 1 == 1:
		if A[0] >> i & 1 == 1 and f:
			tmp = 1 << i
			ans += ttmp
			ttmp = 0
			f = False
		elif A[0] >> i & 1 == 0:
			ttmp -= 1 << i
	elif A[0] >> i & 1 == 1:
		ans += 1 << i
		tmp = 0
		ans += ttmp
		ttmp = 0
		f = False
if f:
	print(-1)
elif ans + tmp >= A[0]:
	print(-1)
else:
	print(ans + tmp)


N = int(input())
A = list(map(int, input().split()))
X = 0
for i in range(2, N):
	X ^= A[i]
if X > A[0] + A[1]:
	print(-1)
else:
	Y = A[0] + A[1] - X
	bY = bin(Y)[2:][::-1]
	bX = bin(X)[2:][::-1]
	if bY[0] == '1':
		print(-1)
	else:
		flag = True
		for i in range(1, len(bY)):
			if bY[i] == '1':
				if i - 1 < len(bX):
					if bX[i - 1] == '1':
						flag = False
						break
		if flag:
			if Y // 2 > A[0]:
				print(-1)
			else:
				a = Y // 2
				for i in range(len(bX)):
					if bX[len(bX) - i - 1] == '1':
						if a + 2 ** (len(bX) - i - 1) <= A[0]:
							a += 2 ** (len(bX) - i - 1)
				if a != 0:
					print(A[0] - a)
				else:
					print(-1)
		else:
			print(-1)


N = int(input())
A = list(map(int, input().split()))
X = 0
for i in range(2, N):
	X ^= A[i]
S = A[0] + A[1]
T = S - X
Y = 0
for i in range(41):
	if 1 << i + 1 & T:
		Y += 1 << i
if Y ^ S - Y ^ X:
	print(-1)
	exit()
for i in range(41, -1, -1):
	if X & 1 << i and Y + (1 << i) <= A[0]:
		Y += 1 << i
if 0 < Y <= A[0]:
	print(A[0] - Y)
else:
	print(-1)


from functools import lru_cache
(N,) = map(int, input().split())
X = list(map(int, input().split()))
t = 0
for x in X:
	t = t ^ x
inf = float('inf')

@lru_cache(None)
def it(x, y, z):
	(xm, ym, zm) = (x % 2, y % 2, z % 2)
	if xm ^ ym != zm:
		return inf
	if x ^ y == z:
		return 0
	Rz = 2 * it(x >> 1, y >> 1, z >> 1)
	Ro = inf
	if x:
		Ro = 2 * it(x - 1 >> 1, y + 1 >> 1, z >> 1) + 1
	Rz = min(Rz, Ro)
	return Rz
r = it(X[0], X[1], t ^ X[0] ^ X[1])
if 0 <= r < X[0]:
	print(r)
else:
	print(-1)


def main(n, a):
	s = a[0] + a[1]
	z = 0
	for ai in a[2:]:
		z ^= ai
	if s - z < 0 or (s - z) % 2 == 1:
		return -1
	d = (s - z) // 2
	if d & z != 0:
		return -1
	if not d <= a[0]:
		return -1
	part = []
	i = 0
	while z // pow(2, i):
		if z // pow(2, i) % 2 == 1:
			part.append(pow(2, i))
		i += 1
	ans = float('inf')
	k = len(part)
	x = 0
	while part:
		tmp = part.pop()
		if d ^ x + tmp <= a[0]:
			x += tmp
	return a[0] - (d ^ x) if d ^ x else -1
n = int(input())
a = list(map(int, input().split()))
print(main(n, a))


N = int(input())
A = list(map(int, input().split()))
xorValue = 0
sumValue = A[0] + A[1]
for i in range(2, N):
	xorValue ^= A[i]
if sumValue % 2 != xorValue % 2:
	print(-1)
else:
	Base = (sumValue - xorValue) // 2
	if Base > A[0]:
		print(-1)
	else:
		strXOR = bin(xorValue)[2:]
		minStoneMovement = A[0] - Base
		strBase = bin(Base)[2:]
		lenStrXOR = len(strXOR)
		cannotMove = False
		if Base < 0:
			cannotMove = True
		for i in range(-1, -1 * min(len(strXOR), len(strBase)) - 1, -1):
			if strBase[i] == '1' and strXOR[i] == '1':
				cannotMove = True
				break
		if not cannotMove:
			for i in range(lenStrXOR):
				if strXOR[i] == '1' and minStoneMovement >= 2 ** (lenStrXOR - 1 - i):
					minStoneMovement -= 2 ** (lenStrXOR - 1 - i)
		if minStoneMovement == A[0] or cannotMove:
			print(-1)
		else:
			print(minStoneMovement)


from sys import exit
n = int(input())
a = list(map(int, input().split()))
X = 0
for i in range(2, n):
	X ^= a[i]
A = (a[0] + a[1] - X) // 2
if a[0] + a[1] - X != A * 2 or A > a[0] or A < 0 or X & A:
	print(-1)
	exit()
res = A
for i in range(42, -1, -1):
	if X >> i & 1:
		if res + (1 << i) <= a[0]:
			res += 1 << i
if res == 0:
	print(-1)
	exit()
print(a[0] - res)


n = int(input())
a = list(map(int, input().split()))
x = 0
for i in a[2:]:
	x ^= i
m = (a[0] + a[1] - x) // 2
if a[0] + a[1] != x + m * 2 or m > a[0] or x & m != 0:
	print(-1)
	return ()
l = 1 << max(x.bit_length(), m.bit_length()) + 1
while l > 0:
	if x & l != 0 and m | l <= a[0]:
		m |= l
	l >>= 1
print(a[0] - m if m != 0 else -1)


import sys
N = int(input())
A = list(map(int, input().split()))
c = 0
for i in range(2, N):
	c ^= A[i]
a = A[0]
b = A[1]
remc = a + b
ori = 0
lis = [None] * 60
for i in range(59, -1, -1):
	np = 2 ** i
	if np & c > 0:
		lis[i] = -1
		remc -= np
	elif remc >= np * 2:
		remc -= np * 2
		lis[i] = 1
		ori |= np
	else:
		lis[i] = 0
if remc != 0 or ori > a:
	print(-1)
	sys.exit()
ao = ori
for i in range(59, -1, -1):
	np = 2 ** i
	if lis[i] == -1 and ao | np <= a:
		ao |= np
if ao > 0:
	print(a - ao)
else:
	print(-1)


import sys
sys.setrecursionlimit(10 ** 7)
import math
from copy import copy, deepcopy
from copy import deepcopy as dcp
from operator import itemgetter
from bisect import bisect_left, bisect, bisect_right
from collections import deque
from collections import Counter
from itertools import accumulate, combinations, permutations
from heapq import heapify, heappop, heappush
from functools import lru_cache
from decimal import Decimal

def input():
	x = sys.stdin.readline()
	return x[:-1] if x[-1] == '\n' else x

def printl(li):
	_ = print(*li, sep='\n') if li else None

def argsort(s, return_sorted=False):
	inds = sorted(range(len(s)), key=lambda k: s[k])
	if return_sorted:
		return (inds, [s[i] for i in inds])
	return inds

def alp2num(c, cap=False):
	return ord(c) - 97 if not cap else ord(c) - 65

def num2alp(i, cap=False):
	return chr(i + 97) if not cap else chr(i + 65)

def matmat(A, B):
	(K, N, M) = (len(B), len(A), len(B[0]))
	return [[sum([A[i][k] * B[k][j] for k in range(K)]) for j in range(M)] for i in range(N)]

def matvec(M, v):
	(N, size) = (len(v), len(M))
	return [sum([M[i][j] * v[j] for j in range(N)]) for i in range(size)]

def T(M):
	(n, m) = (len(M), len(M[0]))
	return [[M[j][i] for j in range(n)] for i in range(m)]

def main():
	mod = 1000000007
	N = int(input())
	A = tuple(map(int, input().split()))
	ref = 0
	for i in range(2, N):
		ref ^= A[i]
	a0 = A[0]
	a1 = A[1]
	ans = 0
	INF = 10 ** 13

	@lru_cache(None)
	def dfs(a0, a1, ref):
		inf = INF
		if a0 & 1 ^ a1 & 1 != ref & 1:
			return inf
		if a0 < 0:
			return inf
		if ref == 0:
			return inf if a0 < a1 else (a0 - a1) // 2
		a01 = a0 & 1
		a11 = a1 & 1
		ref1 = ref & 1
		ans = inf
		if a01 ^ a11 == ref1:
			ans = min(ans, 2 * dfs(a0 >> 1, a1 >> 1, ref >> 1))
		ans = min(ans, 2 * dfs((a0 - 1) // 2, (a1 + 1) // 2, ref >> 1) + 1)
		return ans
	x = dfs(a0, a1, ref)
	if x >= a0:
		print(-1)
	else:
		print(x)

def __starting_point():
	main()
__starting_point()


N = int(input())
A = list(map(int, input().split()))
XOR = 0
for el in A[2:]:
	XOR ^= el
if (A[0] + A[1] - XOR) % 2 != 0:
	print(-1)
	exit(0)
AND = (A[0] + A[1] - XOR) // 2
if XOR & AND != 0:
	print(-1)
	exit(0)
ok = False
u = 0
v = 0
bits = list()
for bit in range(60, -1, -1):
	if XOR >> bit & 1 == 0 and AND >> bit & 1 == 1:
		u |= 1 << bit
		v |= 1 << bit
	elif XOR >> bit & 1 == 1 and AND >> bit & 1 == 0:
		bits.append(bit)
for bit in bits:
	if u | 1 << bit <= A[0]:
		u |= 1 << bit
if u > A[0] or u == 0:
	print(-1)
	exit(0)
print(A[0] - u)


n = int(input())
a = list(map(int, input().split()))
S = sum(a[:2])
X = 0
for i in range(2, n):
	X ^= a[i]
if S - X < 0 or (S - X) % 2 != 0:
	print(-1)
	exit(0)
D = (S - X) // 2
if D > a[0] or D & X:
	print(-1)
	exit(0)
res = D
for i in range(60, -1, -1):
	if X & 1 << i:
		Q = res | 1 << i
		if Q <= a[0]:
			res = Q
print(-1 if res == 0 else a[0] - res)


import sys
readline = sys.stdin.readline
N = int(readline())
A = list(map(int, readline().split()))
S = A[0] + A[1]
X = 0
for i in range(2, N):
	X ^= A[i]
K = S - X
if K & 1:
	print(-1)
else:
	K >>= 1
	if K & X or K > A[0]:
		print(-1)
	else:
		Y = 0
		for i in range(60, -1, -1):
			if X & 1 << i and Y + (1 << i) ^ K <= A[0]:
				Y += 1 << i
		Y = Y ^ K
		if not Y:
			print(-1)
		else:
			print(A[0] - Y)