from functools import lru_cache

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

X = list(map(int, input().split()))

t = 0

for x in X:

    t = t^x

inf = float('inf')

@lru_cache(None)

def it(x, y, z):

    xm, ym, zm = x%2, y%2, z%2

    if xm ^ ym != zm:

        return inf

    if x^y == z:

        return 0

    Rz = 2*it(x>>1, y>>1, z>>1)

    Ro = inf

    if x:

        Ro = 2*it((x-1)>>1, (y+1)>>1, z>>1)+1

    Rz = min(Rz, Ro)

    return Rz



r = it(X[0], X[1], t^X[0]^X[1])

if 0 <= r < X[0]:

    print(r)

else:

    print((-1))

import sys

N, = list(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)))

from functools import lru_cache

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


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


r = it(X[0], X[1], t ^ X[0] ^ X[1])
if 0 <= r < X[0]:
    print(r)
else:
    print((-1))

import sys

(N,) = list(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)))

-from functools import lru_cache +import sys -t = 0 -for x in X: -    t = t ^ x -inf = float("inf") - - -@lru_cache(None) -def it(x, y, z): -    xm, ym, zm = x % 2, y % 2, z % 2 -    if xm ^ ym != zm: -        return inf -    if x ^ y == z: -        return 0 -    Rz = 2 * it(x >> 1, y >> 1, z >> 1) -    Ro = inf -    if x: -        Ro = 2 * it((x - 1) >> 1, (y + 1) >> 1, z >> 1) + 1 -    Rz = min(Rz, Ro) -    return Rz - - -r = it(X[0], X[1], t ^ X[0] ^ X[1]) -if 0 <= r < X[0]: -    print(r) +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)) -    print((-1)) +    print((X[0] - (Y ^ D)))