program main
    use iso_fortran_env
    implicit none
    integer(int32) :: n, i
    integer(int64), allocatable :: a(:)
    integer(int64) :: s, x, d, ans
    logical :: bx, bd

    read *, n
    allocate(a(n))
    read *, a(1:n)

    x = 0
    d = 0
    s = a(1) + a(2)
    ans = 0

    if ( n>2 ) then
        do i = 3, n
            x = ieor(x, a(i))
        enddo
    endif

    if ( trailz(s-x) >= 1 ) then
        d = (s - x) / 2
    else
        print *, "-1"
        stop
    endif

    if ( iand(x, d) /= 0) then
        print *, "-1"
        stop
    endif

    do i = 0, 63
        bd = btest(d, i)
        if (bd) then
            ans = ibset(ans, i)
        endif
    enddo

    do i = 63, 0, -1
        bx = btest(x, i)
        if (bx) then
            if ( ibset(ans, i) <= a(1) ) then
                ans = ibset(ans, i)
            endif
        endif
    enddo

    ans = a(1) - ans

    if (ans <= 0) then
        print *, "-1"
    else
        print *, ans
    endif

end program main
