You are given N piles of stones, each with a different number of stones. Aoki and Takahashi take turns removing stones from a single pile. The player unable to make a move loses. Takahashi, the second player, aims to guarantee his win by moving 0 to (A1 - 1) stones from the first pile to the second before the game starts. If possible, print the minimum number of stones to move; otherwise, print -1. Constraints: 2 ≤ N ≤ 300, 1 ≤ Ai ≤ 10^12. Input format: N followed by A1...AN. Output the minimum number of stones to move or -1. Examples: (1) Input: 2, 5 3; Output: 1. (2) Input: 2, 3 5; Output: -1. (3) Input: 3, 1 1 2; Output: -1. (4) Input: 8, 10 9 8 7 6 5 4 3; Output: 3. (5) Input: 3, 4294967297 8589934593 12884901890; Output: 1. Note: Watch out for overflows.

# Buggy version:
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] * 40
    K = xorR
    for i in range(40):
        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
        K = D
        dbit = [0] * 40
        for i in range(40):
            dbit[i] = K % 2
            K //= 2
        A0, A1 = 0, 0
        for i in range(40):
            if rbit[39 - i] == dbit[39 - i] == 1:
                print(-1)
                break
            else:
                if rbit[39 - i] == 1:
                    if (A0 + pow(2, 39 - i)) ^ D <= A[0]:
                        A0 += pow(2, 39 - i)
                    else:
                        A1 += pow(2, 39 - i)
        if A0 > 0: print(A[0] - A0)
        else: print(-1)

    return 0

if __name__ == "__main__":
    solve()

# Fixed version:
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
            else:
                if 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

if __name__ == "__main__":
    solve()

# Buggy version:
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*2or m>a[0]or(m&x)!=0:exit(print(-1))
l=1<<max(x.bit_length(),m.bit_length())
while l>0:
 if (x&l)!=0and(m|l)<=a[0]:m|=l
 l>>=1
print(a[0]-m if a!=0else-1)

# Fixed version:
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*2or m>a[0]or(x&m)!=0:exit(print(-1))
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!=0else-1)

# Buggy version:
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
ans = a - d
print(ans)

# Fixed version:
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)

# Buggy version:
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]:
    print(-1)
    exit(0)
print(A[0] - u)

# Fixed version:
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)

# Buggy version:
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)
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)

# Fixed version:
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)

# Buggy version:
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()
M = T.bit_length()+1
if D > X[0]:
    print(-1)
    sys.exit()
Y = 0
for i in range(M, -1, -1):
    if T & (1<<i) and ((1<<i)|Y)^D <= X[0]:
        Y = (1<<i)|Y
if a == 0:
    print(-1)
else:
    print(X[0]-a)


# Fixed version:
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)

# Buggy version:
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()
M = S.bit_length()+1
if D > X[0]:
    print(-1)
    sys.exit()
a = 0
for i in range(M, -1, -1):
    if (T >> i)&1 and ((1<<i)|a)^D <= X[0]:
        a = (1<<i)|a
if a == 0:
    print(-1)
else:
    print(X[0]-a)


# Fixed version:
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()
M = T.bit_length()+1
if D > X[0]:
    print(-1)
    sys.exit()
Y = 0
for i in range(M, -1, -1):
    if T & (1<<i) and ((1<<i)|Y)^D <= X[0]:
        Y = (1<<i)|Y
if Y^D == 0:
    print(-1)
else:
    print(X[0]-(Y^D))

# Buggy version:
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 < 10**18:
    print(r)
else:
    print(-1)


# Fixed version:
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)

# Buggy version:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
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 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 == a[0]:
        print(-1)
        exit()
    else:
        print(a[0]-a1)

    return

if __name__ == '__main__':
    main()


# Fixed version:
import math
#import numpy as np
import queue
from collections import deque,defaultdict
import heapq as hpq
from sys import stdin,setrecursionlimit
#from scipy.sparse.csgraph import dijkstra
#from scipy.sparse import csr_matrix
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

if __name__ == '__main__':
    main()

# Buggy version:
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:
            ans += a << i
    i -= 1
print(a[0]-ans)

# Fixed version:
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)

# Buggy version:
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]:
    sys.exit()

for i in reversed(range(bit_len)):
    if t & (1 << i):
        if a1 + 2 ** i <= a[0]:
            a1 += 2 ** i

if a1 == 0:
    print(-1)
else:
    print(a[0]-a1)
    



# Fixed version:
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)

# Buggy version:
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
        strMinMove = bin(minStoneMovement)[2:]
        lenStrXOR = len(strXOR)
        cannotMove = False
        if len(strMinMove) > len(strXOR):
            cannotMove = True
        else:
            for i in range(lenStrXOR):
                if strXOR[i] == '0' and i >= len(strXOR) - len(strMinMove) and strMinMove[i + len(strMinMove) - len(strXOR)] == '1':
                    cannotMove = True
                    break
                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)

# Fixed version:
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)

# Buggy version:
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)
    exit()

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)
        exit()
    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:
    print(-1)
else:
    print(a - n)

# Fixed version:
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)
    exit()

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)
        exit()
    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)

# Buggy version:
# coding: utf-8
# Your code here!
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 or z<0 or z&w:
    print(-1)
else:
    z //= 2
    """
    z = p&q
    v = p^q
    """
    #print(z,v,z&w)
    ans = z
    for i in range(60,-1,-1):
        m = 1<<i
        if v&m:
            if ans+m <= a[0]:
                ans += m
                #print(ans,i)

    if ans >= a[0] or ans==0: print(-1)
    else: print(a[0]-ans)        
        
        
        
        
            






# Fixed version:
# coding: utf-8
# Your code here!
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
    """
    z = p&q
    v = p^q
    """
    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
                #print(ans,i)

    if ans > a[0] or ans==0: print(-1)
    else: print(a[0]-ans)