import Data.Bits
import Data.Maybe
import qualified Data.ByteString.Char8 as B
main=print.solve.map(fst.fromJust.B.readInt).B.words=<<B.getContents
solve(n:a1:a2:as)=if e then -1 else a1-r where
  s=a1+a2
  x=foldr xor 0 as
  d=(s-x)`div`2
  e=s-x<0 || (s-x)`mod`2/=0 || d.&.x/=0 || r==0 || r>a1
  r=d`xor`foldr f 0[0..63]
  f i y
    |testBit x i && setBit y i`xor`d<=a1=setBit y i
    |otherwise=y