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: interview

Starter code:

Solutions:
n = int(input())
a = list(map(int, input().split()))

if n == 2:
  if a[0] >= a[1] and (a[0] - a[1]) % 2 == 0:
    print((a[0] - a[1]) // 2)
  else:
    print(-1)
else:
  num = 0
  for i in range(2, n):
    num ^= a[i]
  
  _and = (a[0] + a[1] - num)
  if  _and % 2 != 0 or a[0] < _and // 2 or (_and // 2) & num != 0:
    print(-1)
  else:
    _and //= 2
    
    max_2 = 1
    while max_2 <= num:
      max_2 *= 2
    
    a0 = _and
    while max_2 >= 1:
      if num & max_2 != 0 and a0 + max_2 <= a[0]:
        a0 += max_2
      max_2 //= 2
    
    if a0 != 0:
      print(a[0] - a0)
    else:
      print(-1)

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
    # d^tmp はd&x=0からd|tmpと一緒
    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)



def __starting_point():
    main()
__starting_point()

import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    _ = int(sys.stdin.readline().rstrip())
    Arr = [int(x) for x in sys.stdin.readline().rstrip().split()]

    K = 43

    #
    A = Arr[0]
    B = Arr[1]

    S = A + B
    X = 0
    for i in range(2, len(Arr)):
        X ^= Arr[i]

    # print(S, X)

    # 次を満たすa, b が見つかるか？
    #  a+b=S, a^b=X, a<=A

    # 桁DP : 桁を一つずつ決めていくようなDP
    # dp[今の桁数 = 0:42][繰り上がりがあるか = 0,1][今までがA以下かどうか 0=以下、1=より大きい] = 最大値
    # 値は最大値。満たすものがなければ -1
    dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

    dp[0][0][0] = 0  # 最初

    v = 1
    for i in range(K - 1):  # 桁数
        cx = X & 1  # current X
        cs = S & 1  # current S
        ca = A & 1  # current A
        for j in range(2):  # 繰り上がりある／ない
            for k in range(2):  # 今までの範囲が A 以下かどうか
                if dp[i][j][k] == -1:  #
                    continue

                for na in range(2):
                    for nb in range(2):
                        ni = i + 1
                        nj = 0
                        nk = k
                        if na ^ nb != cx:  # a ^ b = A か？
                            continue

                        ns = na + nb + j
                        if ns % 2 != cs:  # a + b = S か？
                            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] | (v * na))

        X >>= 1
        S >>= 1
        A >>= 1
        v <<= 1
        # print(v)

    a = dp[K - 1][0][0]
    if a == -1 or a == 0:
        print((-1))
    else:
        print((Arr[0] - a))


main()


def main(n,a):
    s=a[0]+a[1]
    z=0
    for ai in a[2:]:
        z^=ai
    # i+j=s,i<=a[0],i^j=z となるi,jを探す
    # i+j=i^j+2*(i & j)
    # s = z + 2*(i & j)
    # i & j = (s-z)/2 = d
    # z = 2*(i & j) - s = i ^ j
    # d = (s-z) / 2
    # z = x^y , x&y=0
    # d^x,d^yは条件を満たす。d^x+d^y=d^x^d^y + 2*((d^x)&(d^y))= z + 2*d = s
    # (d^x)^(d^y)=x^y=z
    # (d^x)&(d^y)=d (z & d = 0 のときのみ)
    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))

import sys
N = int(input())
A = list(map(int,input().split()))
ALL = 0
for a in A:
    ALL ^= a

if ALL == 0:
    print(0)
    return

ALL ^= A[0] ^ A[1]

K = A[0] + A[1]
L = []

for i in range(40, -1, -1):
    if (1 << i) & ALL:
        L.append(1)
        K -= (1 << i)
    else:
        if K >= 2 * (1 << i):
            L.append(2)
            K -= 1 << (i + 1)
        else:
            L.append(0)

if K != 0:
    print(-1)
    return

ans = 0
for i in range(41):
    if L[i] == 2:
        ans += 1 << (40 - i)

if ans >= A[0]:
    print(-1)
    return

for i in range(41):
    if L[i] == 1:
        if ans + (1 << (40 - i)) < A[0]:
            ans += 1 << (40 - i)

if ans == 0:
    print(-1)
    return

print(A[0] - ans)

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

XOR=0
for a in A[2:]:
    XOR^=a

S=A[0]+A[1]

DP=[[[1<<60]*2 for i in range(2)] for j in range(60)]
DP[0][0][1]=0

for i in range(59):
    CX=0
    if XOR & (1<<i)!=0:
        CX=1

    CS=0
    if S & (1<<i)!=0:
        CS=1

    CA=0
    if A[1] & (1<<i)!=0:
        CA=1
    
    for j in range(2):
        for k in range(2):
            if DP[i][j][k]==1<<60:
                continue

            for na,nb in [(0,0),(0,1),(1,0),(1,1)]:
                if na^nb != CX:
                    continue
                if (na+nb+j)%2 != CS:
                    continue
                
                if na+nb+j>=2:
                    nj=1
                else:
                    nj=0

                if CA<nb:
                    nk=1
                elif CA==nb:
                    nk=k
                else:
                    nk=0

                DP[i+1][nj][nk]=min(DP[i+1][nj][nk],DP[i][j][k]|((1<<i)*nb))
                #print(i,j,k,nj,nk,DP[i][j][k]|((1<<i)*nb))

ANS=DP[59][0][1]-A[1]

if ANS>=A[0]:
    print((-1))
else:
    print(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
    # d^tmp はd&x=0からd+tmpと一緒
    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)



def __starting_point():
    main()
__starting_point()


def resolve():
    # 後手必勝になる移動個数の最小を求める
    # 移動して A1xorA2 == A3~AN xor になるか
    # 移動個数を1つずつ試すとTLE なのでA1+A2を2つに分けて条件を満たす方法を探す
    N = int(input())
    A = list(map(int, input().split()))

    # A1 + A2
    S = A[0] + A[1]

    # 3～N番目のxor
    xor = 0
    for a in A[2:]:
        xor ^= a

    # S == A1xorA2 + 2(A1&A2) -> 2(A1&A2) == S - A3~AN xor
    # 2で割れなければ不可能
    S -= xor
    if S%2 == 1:
        return print(-1)

    S //= 2
    # A1&A2 と A1xorA2が同じ箇所のビットが立っていたら不可能
    if (S&xor) != 0 or S > A[0]:
        return print(-1)

    # A1 に残す個数は最低A1&A2であり、A1xorA2を上の桁から貪欲に、
    # i桁目が1かつ、減らす個数に加えてもAi以上にならないように加えていく
    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()

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
    #print(t)
    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

    #print(a0,a1)
    if a0>0 and a0<=aa[0]:print(aa[0]-a0)
    else:print(-1)

main()


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
    # d^tmp はd&x=0からd|tmpと一緒
    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)



def __starting_point():
    main()
__starting_point()

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)
  return
d = (s-x)//2
if d&x:
  print(-1)
  return
if d > a:
  print(-1)
  return
for i in range(50, -1, -1):
  if (x>>i)&1 and d|(1<<i) <= a:
    d |= 1<<i
if d == 0:
  print(-1)
  return
ans = a - d
print(ans)

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


# def sub(kouho, target):
#     l = len(kouho)
#     best = None
#     for b in range(1<<l):
#         v = 0
#         for i in range(l.bit_length()):
#             if b>>i&1:
#                 v += 1<<i
#         if 
                

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)

import sys
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
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#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
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
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    ref=0
    for i in range(2,N):
        ref^=A[i]
    
    a0=A[0]
    a1=A[1]
    ans=0
    #print(bin(a0),bin(a1),bin(ref))
    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)
    # for j in range(0,41):
    #     a0j=(a0>>j)&1
    #     a1j=(a1>>j)&1
        
    #     refj=(ref>>j)&1
    #     #print(a0j,a1j,refj)
    #     if a0j^a1j==refj:
    #         nref=(ref>>(j+1))&1

    #         na0=((a0>>(j+1))&1)
    #         na1=((a1>>(j+1))&1)
            
    #         if ((a0>>(j+1))&1)^((a1>>(j+1))&1)!=nref:
    #             #print("a",na0,na1,nref)
    #             shif=1<<j
    #             if a0>0 and (((a0-shif))^(((a1+shif))==ref:
    #                 ans+=shif
    #                 a0-=shif
    #                 a1+=shif


    #         continue
    #     else:
    #         shif=1<<j

    #         ans+=shif
    #         a0-=shif
    #         a1+=shif
    #         #print(shif,a0,a1)
    #         if a0<0:
    #             print(-1)
    #             return









def __starting_point():
    main()
__starting_point()

from functools import reduce, lru_cache

INF = float("inf")

N, a, b, *A = map(int, open(0).read().split())
c = reduce(lambda a, b: a ^ b, A, 0)

@lru_cache(None)
def solve(a, b, c):
    if (a % 2) ^ (b % 2) != (c % 2):
        return INF
    if c == 0:
        return INF if a < b else (a - b) // 2

    return min(
        2 * solve(a // 2, b // 2, c // 2),
        1 + 2 * solve((a - 1) // 2, (b + 1) // 2, c // 2)
    )

print(-1 if (res := solve(a, b, c)) >= a else res)

n = int(input())
A = list(map(int,input().split()))
a = A[0]
b = A[1]
x = 0
for i in A[2:]:
    x ^= i
 
d = {}
def F(a,b,xor):
    if (a,b,xor) in d:
        return d[(a,b,xor)]
    if a&1 ^ b&1 != xor&1:
        return float("INF")
    if xor == 0:
        if a<b:
            return float("INF")
        else:
            return (a-b)//2
    c1 = 2*F(a//2,b//2,xor//2)
    c2 = 2*F((a-1)//2,(b+1)//2,xor//2) + 1
    f = min(c1,c2)
    d[(a,b,xor)] = f
    return f
 
ans = F(a,b,x)
if ans >= a:
    print(-1)
else:
    print(ans)

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

def __starting_point():
    solve()
__starting_point()

import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


# def sub(kouho, target):
#     l = len(kouho)
#     best = None
#     for b in range(1<<l):
#         v = 0
#         for i in range(l.bit_length()):
#             if b>>i&1:
#                 v += 1<<i
#         if 
                

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 or v1>a[0]:
                ans = -1
            else:
                ans = a[0]-v1
        else:
            ans = -1
        
print(ans)

from functools import lru_cache

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

tmp = 0
for a in A[2:]:
    tmp ^= a

INF = 10 ** 18

@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 #xor = 0にできるのは aとbが同じときだけ --> aのほうが大きい必要がある
    #1の位を放置
    x = 2 * F(a // 2, b // 2, xor // 2)
    #1の位を移動
    y = 2 * F((a - 1) // 2, (b + 1) // 2, xor // 2) + 1 # +1 は移動コスト

    x = min(x, y)
    if x >= INF:
        x = INF
    
    return x

x = F(A[0], A[1], tmp)
if x >= A[0]:
    x = -1

print (x)

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

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: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!=0else-1)

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()))
X=0
if N>2:
    X=A[2]
for i in range(3,N):
    X=(X|A[i])-(X&A[i])
S=A[0]+A[1]
if (S-X)%2==1 or (S-X)<0:
    print(-1)
else:
    D=(S-X)//2
    if D&X>0:
        print(-1)
    else:
        a=D
        for i in range(50,-1,-1):
            if ((X>>i)&1) and ((a+(1<<i))<A[0]):
                a+=(1<<i)
        if a>0 and A[0]>=a:
            print(A[0]-a)
        else:
            print(-1)

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
    # d^tmp はd&x=0からd|tmpと一緒
    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)



def __starting_point():
    main()
__starting_point()

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

# a = A_1, b = A_2, x = A_3 ^ ... ^ A_N, s = a + b とする
a, b = A[0], A[1]
x = 0
for i in range(2, N):
    x ^= A[i]
s = a + b

M = 42
# dp[桁][繰り上げフラグ][これまでの桁でAより大きいフラグ] := Aとなり得る値の最大値
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):
                    # 条件1: na ^ nb = x
                    if na ^ nb != cx:
                        continue
                    # 条件2: na + nb = S
                    ns = na + nb + j
                    if ns & 1 != cs:
                        continue
                    
                    # 遷移先のi, j, kを求める
                    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))


import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    f, s, *a = nm()
    x = 0
    for c in a: x ^= c
    if (f + s - x) % 2 or ((f + s - x) // 2) & x:
        print(-1)
        return
    y = (f + s - x) // 2
    for i in range(40, -1, -1):
        if x & (1 << i) and y | (1 << i) < f:
            y |= 1 << i
    print(f - y if f >= y > 0 else -1)
    return

solve()


def main():
    n=int(input())
    A=list(map(int,input().split()))
    x=A[0]^A[1]
    for i in A:
        x^=i
    s=A[0]+A[1]
    dp=[[[-1]*2 for _ in range(2)] for _ in range(43)]
    dp[0][0][0]=0
    v=1
    a=A[0]
    for i in range(42):
        cx,ca,cs=x&1,a&1,s&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
                        ni,nj,nk,ns=i+1,0,k,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]|(v*na))
        x>>=1
        s>>=1
        a>>=1
        v<<=1
    a=dp[42][0][0]
    if a==0 or a==-1:
        ans=-1
    else:
        ans=A[0]-a
    print(ans)

def __starting_point():
    main()
__starting_point()

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

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

n = A[0] + A[1] - xor
if n < 0 or n%2:
    print(-1)
    return

n //= 2
a = b = 0
digit = 1
d = []
while n or xor:
    n0 = n & 1; xor0 = xor & 1
    if n0:
        if xor0:
            print(-1)
            return
        else:
            a += digit
            b += digit
    else:
        if xor0:
            b += digit
            d.append(digit)

    digit *= 2
    n >>= 1; xor >>= 1

if a > A[0]:
    print(-1)
    return

while d:
    dd = d.pop()
    if a + dd <= A[0]:
        a += dd; b -= dd

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

# F - Unfair Nim

N = int(input())
A = list(int(a) for a in input().split())

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

dp = [[[-1] * 2 for i in range(2)] for j in range(50)]
dp[0][0][0] = 0

for i in range(49):
    cur_s = S>>i&1
    cur_x = X>>i&1
    cur_a = a>>i&1
    for j, k in ((0, 0), (0, 1), (1, 0), (1, 1)):
        if dp[i][j][k] == -1:
            continue
        for p, q in ((0, 0), (0, 1), (1, 0), (1, 1)):
            if p ^ q != cur_x:
                continue
            if (p + q + j)&1 != cur_s:
                continue
            ni = i + 1
            nj = 0
            if (p + q + j) >= 2:
                nj = 1
            if cur_a < p:
                nk = 1
            elif cur_a > p:
                nk = 0
            else:
                nk = k
            dp[ni][nj][nk] = max(dp[ni][nj][nk], dp[i][j][k]|(1<<i)*p)

tmp = dp[49][0][0]
if tmp == 0 or tmp == -1:
    print(-1)
else:
    print(a - tmp)

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 = '' # <= A[0] for sure.
left_stones_e_A0 = '' #not sure if this is <= A[0].
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'
    else:
        if 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
sys.setrecursionlimit(10**7) #再帰関数の上限,10**5以上の場合python
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#2分探索
#bisect_left(l,x), bisect(l,x)#aはソート済みである必要あり。aの中からx未満の要素数を返す。rightだと以下
from collections import deque
#deque(l), pop(), append(x), popleft(), appendleft(x)
#q.rotate(n)で → にn回ローテート
from collections import Counter#文字列を個数カウント辞書に、
#S=Counter(l),S.most_common(x),S.keys(),S.values(),S.items()
from itertools import accumulate,combinations,permutations#累積和
#list(accumulate(l))
from heapq import heapify,heappop,heappush
#heapify(q),heappush(q,a),heappop(q) #q=heapify(q)としないこと、返り値はNone
#import fractions#古いatcoderコンテストの場合GCDなどはここからimportする
from functools import lru_cache#pypyでもうごく
#@lru_cache(maxsize = None)#maxsizeは保存するデータ数の最大値、2**nが最も高効率
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
    #w.sort(key=itemgetter(1),reversed=True)  #二個目の要素で降順並び替え

    N = int(input())
    #N, K = map(int, input().split())
    A = tuple(map(int, input().split())) #1行ベクトル
    #L = tuple(int(input()) for i in range(N)) #改行ベクトル
    #S = tuple(tuple(map(int, input().split())) for i in range(N)) #改行行列
    ref=0
    for i in range(2,N):
        ref^=A[i]
    
    a0=A[0]
    a1=A[1]
    s=a0+a1

    # ans=0
    # #print(bin(a0),bin(a1),bin(ref))
    # 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)
    
    ##桁dp
    dp=[[[-1]*2 for _ in range(2)] for _ in range(43)]
    refi=ref
    si=s
    dp[0][0][0]=0
    shift=1
    a=a0
    for i in range(42):
        ni=i+1
        for j in range(2):
            for k in range(2):
                cur=dp[i][j][k]
                if cur==-1:
                    continue
                for ai in range(0,2):
                    bi=(refi&1)^ai
                    if (bi+j+ai)&1!=si&1:
                        continue
                    if ai>a&1:
                        nk=1
                    elif ai==a&1:
                        nk=k
                    else:
                        nk=0

                    nj=0
                    if bi+j+ai>1:
                        nj=1
                    
                    dp[ni][nj][nk]=max(dp[ni][nj][nk],cur|shift*ai)
                    
        refi=(refi>>1)
        si>>=1
        a>>=1
        shift<<=1
    x=dp[-1][0][0]
    if x==-1 or x==0:
        print(-1)
    else:
        print(a0-x)


                





def __starting_point():
    main()
__starting_point()

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)

N=int(input())
A=list(map(int,input().split()))
S=A[0]+A[1]
X=0
for i in range(2,N):
    X=X^A[i]
if (S-X)%2==1 or S<X:
    print((-1))
    return
D=(S-X)//2
if X&D!=0:
    print((-1))
    return
a=0
#print(X,D)
B=[0 for i in range(42)]
for k in range(41,-1,-1):
    x=(X&(1<<k))>>k
    d=(D&(1<<k))>>k
    if (x,d)==(0,0):
        pass
    elif (x,d)==(0,1):
        a=a^(1<<k)
    elif (x,d)==(1,0):
        B[k]=1
    else:
        pass
for k in range(41,-1,-1):
    if B[k]==0:
        continue
    if a^(1<<k)<A[0]:
        a=a^(1<<k)
if 0<a<=A[0]:
    print((A[0]-a))
else:
    print((-1))


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())
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)
    return
a = (s-x)//2
if a > l[0]:
    print(-1)
    return
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)

import sys

readline = sys.stdin.readline
readall = sys.stdin.read
ns = lambda: readline().rstrip()
ni = lambda: int(readline().rstrip())
nm = lambda: map(int, readline().split())
nl = lambda: list(map(int, readline().split()))
prn = lambda x: print(*x, sep='\n')

def solve():
    n = ni()
    f, s, *a = nm()
    x = 0
    for c in a: x ^= c
    if (f + s - x) % 2 or ((f + s - x) // 2) & x:
        print(-1)
        return
    y = (f + s - x) // 2
    for i in range(40, -1, -1):
        if x & (1 << i) and y | (1 << i) <= f:
            y |= 1 << i
    print(f - y if f >= y > 0 else -1)
    return

solve()


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

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)
            return
    else:
        ans += (D & (1 << i))

if ans > a[0]:
    print(-1)
    return
        
for keta in free_keta[::-1]:
    if ans + (1 << keta) <= a[0]:
        ans += (1 << keta)

if ans == 0:
    print(-1)
    return
    
print(a[0] - ans)

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)


def __starting_point():
    main()

__starting_point()

#解説方針
import math
N = int(input())
A = list(map(int, input().split()))

all_xor = 0
for i in range(N):
  all_xor = all_xor ^ A[i]

fore = A[0] ^ A[1]#S
back = fore ^ all_xor #X
#a + b = (a xor b) + 2 * (a and b)
#a xor b = back, a + b = A[0] + A[1]

a_and_b = (A[0] + A[1] - back) / 2
#print(a_and_b, back)
#a_and_bは非負整数である必要がある
if (a_and_b % 1 != 0) or (a_and_b < 0):
  print((-1))
  return
a_and_b = int(a_and_b)

#print(a_and_b, back, a_and_b & back)
#a_and_bとbackには共通のbitがない
if (a_and_b & back) != 0:
  print((-1))
  return

ans = a_and_b
#print(math.log2(32))
if back == 0:
  if (ans == 0):
    print((-1))
  else:
    print((A[0] - ans))
  return
else:
  if ans > A[0]:
    print((-1))
    return
  nn = int(math.log2(back)) + 1
  #print(nn)
  #print(2 >> 1)
  #print(2 << 1)
  for i in range(nn + 1):
    #print(back, nn - i)
    if ((back >> (nn - i)) & 1):
      #print(ans, 2 << (nn - i))
      k = ans + (1 << (nn - i))
      #print("k", k, i)
      if k <= A[0]:
        ans = k 
  if (ans == 0):
    print((-1))
  else:
    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
    #print(bin(c),bin(d))
    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]
        #print(flgs)
        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)
            
            #print(ans)
            
            if ans==0:
                print((-1))
            else:
                print((A1-ans))
            
                    
                    
            
        


#写経
#https://atcoder.jp/contests/abc172/submissions/14777611

def resolve():
    N = int(input())
    A = [int(a) for a in input().split()]
    x = 0
    for a in A[2:]:
        x ^= a                              # ^はXOR計算
    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    # >> i は右方向iビットシフト
        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()

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)

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)

n = int(input())
A = list(map(int,input().split()))
a = A[0]
b = A[1]
x = 0
for i in A[2:]:
    x ^= i

d = {}
def F(a,b,xor):
    if (a,b,xor) in d:
        return d[(a,b,xor)]
    if a&1 ^ b&1 != xor&1:
        return float("INF")
    if xor == 0:
        if a<b:
            return float("INF")
        else:
            return (a-b)//2
    c1 = 2*F(a//2,b//2,xor//2)
    c2 = 2*F((a-1)//2,(b+1)//2,xor//2) + 1
    f = min(c1,c2)
    d[(a,b,xor)] = f
    return f

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

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)

def __starting_point():
    main()
__starting_point()

import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7


def main():
    _ = int(sys.stdin.readline().rstrip())
    Arr = [int(x) for x in sys.stdin.readline().rstrip().split()]

    K = 43

    #
    A = Arr[0]
    B = Arr[1]

    S = A + B
    X = 0
    for i in range(2, len(Arr)):
        X ^= Arr[i]

    # print(S, X)

    # 次を満たすa, b が見つかるか？
    #  a+b=S, a^b=X, a<=A

    # 桁DP : 桁を一つずつ決めていくようなDP
    # dp[今の桁数 = 0:42][繰り上がりがあるか = 0,1][今までがA以下かどうか 0=以下、1=より大きい] = 最大値
    # 値は最大値。満たすものがなければ -1
    dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

    dp[0][0][0] = 0  # 最初

    v = 1
    for i in range(K - 1):  # 桁数
        cx = X & 1  # current X
        cs = S & 1  # current S
        ca = A & 1  # current A
        for j in range(2):  # 繰り上がりある／ない
            for k in range(2):  # 今までの範囲が A 以下かどうか
                if dp[i][j][k] == -1:  #
                    continue

                for na in range(2):
                    for nb in range(2):
                        ni = i + 1
                        nj = 0
                        nk = k
                        if na ^ nb != cx:  # a ^ b = A か？
                            continue

                        ns = na + nb + j
                        if ns % 2 != cs:  # a + b = S か？
                            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] | (v * na))

        X >>= 1
        S >>= 1
        A >>= 1
        v <<= 1
        # print(v)

    a = dp[K - 1][0][0]
    if a == -1 or a == 0:
        print(-1)
    else:
        print(Arr[0] - a)


main()

def main():
    n = int(input())
    a = list(map(int, input().split()))
    x, y = a[0], a[1]
    nim = 0
    for i in range(2, n):
        nim ^= a[i]
    ans = 0

    while((x ^ y) != nim):
        for i in range(50, -1, -1):
            j = (nim >> i) & 1
            xy = ((x >> i) ^ (y >> i)) & 1
            if j != xy:
                m = min(x % (2**i)+1, 2**i-(y % (2**i)))
                if x % (2**i)+1 == 2**i-(y % (2**i)):
                    print((-1))
                    return
                x -= m
                y += m
                ans += m
                if x <= 0:
                    print((-1))
                    return
                break
        else:
            if (x ^ y) != nim:
                print((-1))
                return
    print(ans)


main()


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

    # check upper part (2**20 <= x)
    ug = g >> 20
    b0 = a0 >> 20
    b1 = a1 >> 20
    upper_ans_0 = -1 # a0: no borrow, a1: no carry
    for i in range(b0):
        t0 = b0 - i
        t1 = b1 + i
        if t0 ^ t1 == ug:
            upper_ans_0 = i
            break

    upper_ans_1 = -1 # a0: no borrow, a1: carry
    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 # a0: borrow, a1: no carry
    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 # a0: borrow, a1: carry
    for i in range(b0-1):
        t0 = b0 - 1 - i
        t1 = b1 + 1 + i
        if t0 ^ t1 == ug:
            upper_ans_3 = i
            break

    # print(upper_ans_0, upper_ans_1, upper_ans_2, upper_ans_3)
    # check lower part (0 ... 2**20)
    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
    # print(len(lower_answers))
    # print(lower_answers)
    answers = []
    for l in lower_answers:
        if (a0 % half) >= l:
            # a0: no borrow
            if (a1 % half) + l < half:
                # no carry
                if upper_ans_0 != -1:
                    answers.append((upper_ans_0 << 20) + l)
            else:
                # carry
                if upper_ans_1 != -1:
                    answers.append((upper_ans_1 << 20) + l)
        else:
            # a0: borrow
            if (a1 % half) + l < half:
                # no carry
                if upper_ans_2 != -1:
                    answers.append((upper_ans_2 << 20) + l)
            else:
                # carry
                if upper_ans_3 != -1:
                    answers.append((upper_ans_3 << 20) + l)
    if len(answers) == 0:
        print((-1))
        return
    answers.sort()
    print((answers[0]))

def __starting_point():
    main()

__starting_point()

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

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

n = A[0] + A[1] - xor
if n < 0 or n%2:
    print(-1)
    return

n //= 2
a = b = 0
digit = [1]
d = []
count = 0    
while n or xor:
    n0 = n & 1; xor0 = xor & 1
    if n0:
        if xor0:
            print(-1)
            return
        else:
            a += digit[-1]
            b += digit[-1]
    else:
        if xor0:
            b += digit[-1]
            d.append(count)

    digit.append(digit[-1] * 2)
    count += 1
    n >>= 1; xor >>= 1

if a > A[0]:
    print(-1)
    return

while d:
    c = d.pop()
    if a + digit[c] <= A[0]:
        a += digit[c]; b -= digit[c]

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

import sys

N_MAX = 200000 + 5
INF = 10**9 + 7
sys.setrecursionlimit(N_MAX)
MOD = 10**9 + 7

n = int(input())
Arr = list(map(int,input().split()))

K = 43

#
A = Arr[0]
B = Arr[1]

S = A + B
X = 0
for i in range(2, len(Arr)):
    X ^= Arr[i]

# print(S, X)

# 次を満たすa, b が見つかるか？
#  a+b=S, a^b=X, a<=A

# 桁DP : 桁を一つずつ決めていくようなDP
# dp[今の桁数 = 0:42][繰り上がりがあるか = 0,1][今までがA以下かどうか 0=以下、1=より大きい] = 最大値
# 値は最大値。満たすものがなければ -1
dp = [[[-1 for x in [0, 1]] for _ in [0, 1]] for _ in range(K)]

dp[0][0][0] = 0  # 最初

v = 1
for i in range(K - 1):  # 桁数
    cx = X & 1  # current X
    cs = S & 1  # current S
    ca = A & 1  # current A
    for j in range(2):  # 繰り上がりある／ない
        for k in range(2):  # 今までの範囲が A 以下かどうか
            if dp[i][j][k] == -1:  #
                continue

            for na in range(2):
                for nb in range(2):
                    ni = i + 1
                    nj = 0
                    nk = k
                    if na ^ nb != cx:  # a ^ b = A か？
                        continue

                    ns = na + nb + j
                    if ns % 2 != cs:  # a + b = S か？
                        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] | (v * na))

    X >>= 1
    S >>= 1
    A >>= 1
    v <<= 1
    # print(v)

a = dp[K - 1][0][0]
if a == -1 or a == 0:
    print(-1)
else:
    print(Arr[0] - a)

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 = 0
b = 2**64
while b>0:
  if (D&b)!=0:
    a |= b
  elif (X&b)!=0:
    if (a|b|D&(b-1))<=A[0]:
      a |= b
  b >>= 1
if a==0:
  print((-1))
  return
print((A[0]-a))


from functools import lru_cache

def main():
    N = int(input())
    A = list(map(int, input().split()))
    x = 0
    for a in A[2:]:
        x ^= a
    a, b = A[0], A[1]
    if (a ^ b ^ x) & 1 != 0:
        return -1

    @lru_cache(None)
    def helper(a, b, x):
        if a == 0:
            return 0 if a ^ b == x else None
        if (a ^ b) & 1 != x & 1:
            return None
        t1 = helper(a // 2, b // 2, x // 2)
        t2 = helper((a - 1) // 2, (b + 1) // 2, x // 2)
        if t1 is None and t2 is None:
            return None
        if t1 is None:
            return t2 * 2 + 1
        if t2 is None:
            return t1 * 2
        t1 = t1 * 2
        t2 = t2 * 2 + 1
        return min(t1, t2)
    r = helper(a, b, x)
    if r is None or r >= a:
        return -1
    return r

print((main()))


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)

INF=10**18
def calc(a,b,xor):
  if (a,b,xor) in memo:
    return memo[(a,b,xor)]
  if (a&1)^(b&1)!=(xor&1):
    return INF
  if xor==0:
    if a<b:
      return INF
    else:
      return (a-b)//2
  x=2*calc(a//2,b//2,xor//2)
  y=2*calc((a-1)//2,(b+1)//2,xor//2)+1
  memo[(a,b,xor)]=min(x,y)
  return memo[(a,b,xor)]

n=int(input())
arr=list(map(int,input().split()))
a,b=arr[0],arr[1]
xor=0
for i in range(2,n):
  xor^=arr[i]
memo={}
ans=calc(a,b,xor)
if ans>=a:
  print(-1)
else:
  print(ans)

# AFTER EDITORIAL


def solve(ls):
    import math

    x, y, *ls = ls
    z = x + y
    s = 0
    for a in ls:
        s ^= a

    #
    # Max a <= x  s.t.
    #   0 = NimSum(a, (x + y) - a, ls)
    # <=>
    # Max a <= x s.t.
    #   ∃b.
    #     a + b = x + y  (NOTE: a + b = a ^ b + 2 (a & b))
    #     a ^ b = NimSum(ls)
    # <=>
    # Max a <= x s.t.
    #   ∃b.
    #     a & b = (x + y - NimSum(ls)) / 2
    #     a ^ b = NimSum(ls)
    #

    #
    # Thus it amounts to solve
    #   a ^ b = s
    #   a & b = (z - s) / 2 = t
    #
    # which is bit-wise independent as
    #   a  b  ^  &
    #   0  0  0  0
    #   0  1  1  0
    #   1  0  1  0
    #   1  1  0  1
    #

    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:  # Greedy is optimal
                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()

# coding: utf-8
# Your code here!
import sys
read = sys.stdin.read
readline = sys.stdin.readline

n,*a = list(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))
        return

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