Problem:
 Score : 600 points    Problem Statement 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 1 2
5 3      Sample Output 1 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.       Sample Input 2 2
3 5      Sample Output 2 -1  It is not allowed to move stones from the 2 -nd pile to the 1 -st.       Sample Input 3 3
1 1 2      Sample Output 3 -1  It is not allowed to move all stones from the 1 -st pile.       Sample Input 4 8
10 9 8 7 6 5 4 3      Sample Output 4 3       Sample Input 5 3
4294967297 8589934593 12884901890      Sample Output 5 1  Watch out for overflows.

Solution (Python):
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)

Solution (Python):
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))

Solution (Python):
import sys
input = sys.stdin.readline

def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))

"""
全部の山のxorを0にしたい
変化する山は2つだけ，1とどこか．
1番目の山からi番目の山に移動するものとする．
この2つの山を使って，うまく目標T（2~Nのうちi番目だけのぞいた全てのxor）を作れるか

2つの山をごちゃ混ぜにして，aとbの二つに分けるものとする
和  :S=A[0] + A[i]
xor :T

Tのbitが立っている場合，aとbの同じ位置では片方しかbitが立っていない
それ以外の場所ではaとbのbitが同じ．

j桁目のbitをxjとする，
bitの立ち方が違うとろの和は当然Tなので
bitの立ち方が同じ桁の総和を考えると
これが S-T　になるので2で割ればどのbitが立つべきかわかる 

例)
5,3,4でi=2なら
5,3をごちゃ混ぜにして，S=8でT=4を作りたい


a= x3  1  x1  x0
b= x3  0  x1  x0

2*(x3+x1+x0) = 8-4
x3 + x1 + x0 =2 
なのでx1だけ立てれば良い

---

1番目の山からしか動かせないので，このa,bが必ずしも作れるとは限らない，
a,bのうち小さい方がA[0]以下なら良い．
片方をできるだけ小さくしたいので，Tで立っているbitはどちらかに固める

"""
def main():
    mod=10**9+7
    inf = 10**13
    
    N=I()
    A=LI()
    allx=0
    for i in range(N):
        allx=allx^A[i]
        
    
    if allx==0:
        print(0)
        exit()
        
    
    if A[0]==1:
        print(-1)
        exit()
        
    
        
    M=45 
        
    def calc(S,T):
        
        
        
        
        
        
        if T>S:
            return False
        if (S-T)%2==1:
            return False
        
        
        
        
        
        
        
        
        if T==S:
            
            cnt=0 
            m=inf 
            T2=T
            a=0
            b=0
            for j in range(M,-1,-1):
                p=1<<j
                if p<=T2:
                    T2-=p
                    m=p
                    cnt+=1
                    if a+p<=A[0]:
                        a+=p
                    else:
                        b+=p
                    
            if cnt==1:
                return False
            if a==0: 
                return False
            if b==0: 
                a-=m 
            return a
        
        
        if (S-T)//2 > A[0]:
            return False
        
        
        T2=T
        S2=(S-T)//2
        a=S2 
        b=S2 
        for i in range(M,-1,-1): 
            p=1<<i
            ft=0
            fs=0
            if p<=T2:
                T2-=p
                ft=1
            if p<=S2:
                S2-=p
                fs=1
                
            if ft and fs:
                return False
            
            if ft:
                if a+p<=A[0]: 
                    a+=p
                else:
                    b+=p
                
            
        return a
    

                 
            
            
        
            

    ans=inf
    for i in range(1,N):
        S=A[0]+A[i]
        T=A[0]^A[i]^allx
        
        a=calc(S,T)
        
        if a!=0:
            
            ans=min(ans,A[0]-a)
            
    if ans==inf:
        ans=-1
    print(ans)
        
    
            
            
            
        
        
        
        
    
    

main()

Solution (Python):
def main():
    n = int(input())
    a = list(map(int, input().split()))
    xor = 0
    for i in range(2, n):
        xor ^= a[i]
    s = a[0] + a[1]
    dp = [[[-1 for k in range(2)] for j in range(2)] for i in range(45)]
    p = a[0]
    dp[0][0][0] = 0
    for i in range(44):
        crrx = xor&1
        crrs = s&1
        crrp = p&1
        for j in range(2):
            for k in range(2):
                if dp[i][j][k] == -1:
                    continue
                for nxtp in range(2):
                    for nxtq in range(2):
                        nxti = i+1
                        nxtj = 0
                        nxtk = k
                        if (nxtp ^ nxtq) != crrx:
                            continue
                        nxts = nxtp+nxtq+j
                        if nxts%2 != crrs:
                            continue
                        if nxts >= 2:
                            nxtj = 1
                        if crrp < nxtp:
                            nxtk = 1
                        elif crrp == nxtp:
                            nxtk = k
                        else:
                            nxtk = 0
                        dp[nxti][nxtj][nxtk] = max(dp[nxti][nxtj][nxtk], dp[i][j][k]+(1<<i)*nxtp)
        xor //= 2
        s //= 2
        p //= 2
    if dp[-1][0][0] > 0:
        print(a[0] - dp[-1][0][0])
    else:
        print(-1)

if __name__ == "__main__":
    main()

Solution (Python):
N = int(input())
A = list(map(int, input().split()))


a, b = A[0], A[1]
x = 0
for i in range(2, N):
    x ^= A[i]
s = a + b

M = 42

dp = [[[-1] * 2 for _ in range(2)] for _ in range(M + 1)]
dp[0][0][0] = 0
v = 1
for i in range(M):
    cs = s & 1
    cx = x & 1
    ca = a & 1
    for j in range(2):
        for k in range(2):
            if dp[i][j][k] == -1:
                continue
            for na in range(2):
                for nb in range(2):
                    
                    if na ^ nb != cx:
                        continue
                    
                    ns = na + nb + j
                    if ns & 1 != cs:
                        continue
                    
                    
                    ni = i + 1
                    nj = 1 if ns >= 2 else 0
                    if na > ca:
                        nk = 1
                    elif na < ca:
                        nk = 0
                    else:
                        nk = k
                    
                    
                    dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k] + na * v)
    s >>= 1
    x >>= 1
    a >>= 1
    v <<= 1

amax = dp[M][0][0]
if amax <= 0:
    print(-1)
else:
    print(A[0] - amax)

Solution (Python):
N = int(input())
A = list(map(int, input().split()))

a, b = A[0], A[1]
X = 0
for i in range(2, N):
    X ^= A[i]
S = a + b

M = 42
dp = [[[-1] * 2 for _ in range(2)] for _ in range(M + 1)]
dp[0][0][0] = 0
v = 1
for i in range(M):
    cs = S & 1
    cx = X & 1
    ca = a & 1
    for j in range(2):
        for k in range(2):
            if dp[i][j][k] == -1:
                continue
            for na in range(2):
                for nb in range(2):
                    
                    if na ^ nb != cx:
                        continue
                    
                    ns = na + nb + j
                    if ns & 1 != cs:
                        continue
                    
                    ni = i + 1
                    nj = 1 if ns >= 2 else 0
                    if na > ca:
                        nk = 1
                    elif na < ca:
                        nk = 0
                    else:
                        nk = k
                    
                    dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k] + na * v)
    S >>= 1
    X >>= 1
    a >>= 1
    v <<= 1

ans = dp[M][0][0]
if ans <= 0:
    print(-1)
else:
    print(A[0] - ans)

Solution (Python):
import sys
INF = float("inf")
MOD = 10**9 + 7 
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from functools import reduce
from operator import xor
from itertools import product
def resolve():
    n = int(input())
    A = list(map(int, input().split())) + [0]
    a = A[0]
    s = sum(A[:2])
    u = reduce(xor, A[2:])

    if s < u or (s + u) & 1:
        print("-1")
        return

    v = (s - u) // 2
    
    dp = [0, -INF]
    for d in range(60, -1, -1):
        ndp = [-INF, -INF]
        ad = a >> d & 1
        ud = u >> d & 1
        vd = v >> d & 1
        for lt, xd, yd in product(range(2), repeat = 3):
            if (not lt) and ad < xd:
                continue
            if xd ^ yd != ud:
                continue
            if xd & yd != vd:
                continue
            nlt = max(lt, ad > xd)
            ndp[nlt] = max(ndp[nlt], dp[lt] + (xd << d))
        dp = ndp

    pile = max(dp)
    if pile <= 0:
        print(-1)
    else:
        print(a - pile)
resolve()

Solution (Python):
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()

Solution (Python):
import sys
from functools import lru_cache

read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N, *A = map(int, read().split())

a, b = A[:2]
xor = 0
for x in A[2:]:
    xor ^= x

INF = 10**13

@lru_cache(None)
def F(a, b, xor):
    if (a & 1) ^ (b & 1) != (xor & 1):
        return INF
    if xor == 0:
        return INF if a < b else (a - b) // 2
    
    x = 2 * F(a // 2, b // 2, xor // 2)
    
    y = 2 * F((a - 1) // 2, (b + 1) // 2, xor // 2) + 1
    x = min(x, y)
    if x >= INF:
        x = INF
    return x

x = F(a, b, xor)
if x >= a:
    x = -1
print(x)

Solution (Python):
import sys
input = sys.stdin.readline

N = int(input())
A = list(map(int, input().split()))
S = A[0]+A[1]
X = 0
A0 = A[0]

for i in range(2, N):
    X ^= A[i]

L = 45
dp = [[[-1]*2 for _ in range(2)] for _ in range(L+1)]
dp[0][0][0] = 0

for i in range(L):
    for j in range(2):
        for k in range(2):
            if dp[i][j][k]==-1:
                continue
            
            for a in range(2):
                for b in range(2):
                    nj, s = divmod(a+b+j, 2)
                    x = a^b
                    
                    if s!=S&1:
                        continue
                    
                    if x!=X&1:
                        continue
                    
                    if a<A0&1:
                        nk = 0
                    elif a>A0&1:
                        nk = 1
                    else:
                        nk = k
                    
                    dp[i+1][nj][nk] = max(dp[i+1][nj][nk], dp[i][j][k]+(a<<i))
    
    S >>= 1
    X >>= 1
    A0 >>= 1

if dp[L][0][0]<=0:
    print(-1)
else:
    print(A[0]-dp[L][0][0])

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):
from functools import lru_cache
import sys
input = sys.stdin.readline

@lru_cache(maxsize=None)
def dfs(a, b, c):
    if (a ^ b) & 1 != c & 1:
        return float('inf')
    if a == 0:
        return 0 if b == c else float('inf')
    return min(2 * dfs(a >> 1, b >> 1, c >> 1), 2 * dfs((a - 1) >> 1, (b + 1) >> 1, c >> 1) + 1)

N = int(input())
A = list(map(int, input().split()))
B = 0
for i in range(2, N):
    B ^= A[i]
ans = dfs(A[0], A[1], B)
print(ans if ans < float('inf') and A[0] > ans else -1)

Solution (Python):
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)

Solution (Python):
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, -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)



if __name__ == '__main__':
    main()

Solution (Python):
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)



if __name__ == '__main__':
    main()

Solution (Python):
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)



if __name__ == '__main__':
    main()

Solution (Python):
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(40, -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)



if __name__ == '__main__':
    main()

Solution (Python):
import numpy as np

LI = lambda: list(map(int, input().split()))

N = int(input())
A = LI()


def main():
    s = 0
    for a in A[2:]:
        s ^= a
    t = A[0] + A[1]
    f = lambda x: list(map(int, format(x, "050b")[::-1]))
    sb = f(s)
    tb = f(t)
    ab = f(A[0])

    dp = np.full((51, 2, 2), -1, dtype=int)
    dp[0][0][0] = 0
    for i in range(50):
        for j in range(2):
            for k in range(2):
                if dp[i][j][k] == -1:
                    continue
                for na in range(2):
                    for nb in range(2):
                        if na ^ nb != sb[i] or (na + nb + j) % 2 != tb[i]:
                            continue
                        nj = (na + nb + j) // 2
                        if na == ab[i]:
                            nk = k
                        elif na == 1:
                            nk = 1
                        else:
                            nk = 0
                        v = dp[i][j][k] + (2 ** i) * na
                        dp[i + 1][nj][nk] = max(dp[i + 1][nj][nk], v)

    if dp[-1][0][0] < 1:
        ans = -1
    else:
        ans = A[0] - dp[-1][0][0]
    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
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)

Solution (Python):
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)

Solution (Python):



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)


if __name__ == "__main__":
    import sys

    main(sys.stdin, sys.stdout)

Solution (Python):

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
    else: 
        if (A[0]>>i)&1==1:
            ans+=1<<i
            tmp=0
            ans+=ttmp
            ttmp=0
            f=False
if f:
    print(-1)
else:
    if ans+tmp>=A[0]:
        print(-1)
    else:
        print(ans+tmp)

Solution (Python):
N = int(input())
A,B,*C = list(map(int,input().split()))
X = 0
for c in C:
    X ^= c
S = A+B

K = 43
dp = [[[-1]*2 for _ in range(2)] for _ in range(K)]
dp[0][0][0] = 0

for i in range(K-1):
    cx = (X>>i)&1
    cs = (S>>i)&1
    ca = (A>>i)&1
    for j in range(2):
        for k in range(2):
            if dp[i][j][k] == -1: continue
            for na in range(2):
                nb = na^cx
                ns = na+nb+j
                if ns%2 != cs: continue
                nj = ns//2
                if ca < na:
                    nk = 1
                elif ca == na:
                    nk = k
                else:
                    nk = 0
                dp[i+1][nj][nk] = max(dp[i+1][nj][nk], dp[i][j][k] + 2**i * na)
a = dp[-1][0][0]
if a==0 or a==-1:
    print(-1)
else:
    print(A - a)

Solution (Python):
N = int(input())
A,B,*C = list(map(int,input().split()))
X = 0
for c in C:
    X ^= c
S = A+B

K = 43
dp = [[[-1]*2 for _ in range(2)] for _ in range(K)]
dp[0][0][0] = 0

for i in range(K-1):
    cx = (X>>i)&1
    cs = (S>>i)&1
    ca = (A>>i)&1
    for j in range(2):
        for k in range(2):
            if dp[i][j][k] == -1: continue
            for na in range(2):
                for nb in range(2):
                    if (na^nb) != cx: continue
                    ns = na+nb+j
                    if ns%2 != cs: continue
                    nj = ns//2
                    if ca < na:
                        nk = 1
                    elif ca == na:
                        nk = k
                    else:
                        nk = 0
                    dp[i+1][nj][nk] = max(dp[i+1][nj][nk], dp[i][j][k] + 2**i * na)
a = dp[-1][0][0]
if a==0 or a==-1:
    print(-1)
else:
    print(A - a)

Solution (Python):
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)

if __name__ == '__main__':
    examF()

"""
142
12 9 1445 0 1
asd dfg hj o o
aidn
"""

Solution (Python):



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