// ${url}
//
#![allow(unused_imports)]
use std::io::*;
use std::io::Write;
use std::fmt::*;
use std::str::*;
use std::cmp::*;
use std::collections::*;

// Input macros.
// Original by tanakh: https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8
#[allow(unused_macros)]
macro_rules! input {
    (source = $s:expr, $($r:tt)*) => {
        let mut iter = $s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
    ($($r:tt)*) => {
        let s = {
            use std::io::Read;
            let mut s = String::new();
            std::io::stdin().read_to_string(&mut s).unwrap();
            s
        };
        let mut iter = s.split_whitespace();
        input_inner!{iter, $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! input_inner {
    ($iter:expr) => {};
    ($iter:expr, ) => {};

    ($iter:expr, $var:ident : $t:tt $($r:tt)*) => {
        let $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };

    ($iter:expr, mut $var:ident : $t:tt $($r:tt)*) => {
        let mut $var = read_value!($iter, $t);
        input_inner!{$iter $($r)*}
    };
}

#[allow(unused_macros)]
macro_rules! read_value {
    ($iter:expr, ( $($t:tt),* )) => {
        ( $(read_value!($iter, $t)),* )
    };

    ($iter:expr, [ $t:tt ; $len:expr ]) => {
        (0..$len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
    };

    ($iter:expr, [ next / $t:tt ]) => {
        {
            let len = read_value!($iter, usize);
            (0..len).map(|_| read_value!($iter, $t)).collect::<Vec<_>>()
        }
    };

    ($iter:expr, switch) => {
        {
            let ty = read_value!($iter, i32);
            if ty == 1 {
                vec![ty, read_value!($iter, i32), read_value!($iter, i32)]
            } else if ty == 2 {
                vec![ty, read_value!($iter, i32)]
            } else {
                vec![ty, read_value!($iter, i32)]
            }
        }
    };

    ($iter:expr, chars) => {
        read_value!($iter, String).chars().collect::<Vec<char>>()
    };

    ($iter:expr, usize1) => {
        read_value!($iter, usize) - 1
    };

    ($iter:expr, $t:ty) => {
        $iter.next().unwrap().parse::<$t>().expect("Parse error")
    };
}

#[allow(unused_macros)]
macro_rules! read_line {
    ($t:tt) => {
        {
            let mut s = String::new();
            std::io::stdin().read_line(&mut s).unwrap();
            s.trim_right().parse::<$t>().unwrap()
        }
    }
}

#[allow(unused_macros)]
macro_rules! dvec {
    ($t:expr ; $len:expr) => {
        vec![$t; $len]
    };

    ($t:expr ; $len:expr, $($rest:expr),*) => {
        vec![dvec!($t; $($rest),*); $len]
    };
}

#[allow(unused_macros)]
macro_rules! ifv {
    ($t:expr, $a:expr, $b: expr) => {
        if $t { $a } else { $b }
    }
}

#[allow(unused_macros)]
macro_rules! fill {
    ($t:expr, $v:expr) => {
        for i in 0..$t.len() {
            $t[i] = $v;
        }
    };
}

#[allow(unused_macros)]
macro_rules! join {
    ($t:expr, $glue:expr) => {
        $t.into_iter().map(|w| w.to_string()).collect::<Vec<_>>().join($glue)
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        eprintln!(concat!($(stringify!($a), " = {:?}, "),*), $($a),*);
    }
}

// ===

const MAX: i64 = 1e18 as i64;

fn main() {
    input! {
        n: usize,
        a: [i64; n]
    };

    let want = (2..n).map(|i| a[i]).fold(0i64, |acc,x| acc^x);

    let mut hidx = 0;
    let mut aa = a[0];
    while aa >= 2 {
        aa /= 2;
        hidx += 1;
    }

    let mut dp = dvec!(MAX; 45, 2, 2);
    dp[0][0][0] = 0;
    for i in 0..=hidx {
        let mask = 1i64<<i;
        for k0 in 0..2 {
            for k1 in 0..2 {
                let base = dp[i][k0][k1];
                if base == MAX {
                    continue;
                }
                let a0 = ifv!(a[0] & mask >= 1, 1, 0) ^ k0;
                let a1 = ifv!(a[1] & mask >= 1, 1, 0) ^ k1;

                let nk0 = ifv!(k0 == 1 && a0 == 1, 1, 0);
                let nk1 = ifv!(k1 == 1 && a1 == 0, 1, 0);


                let flag = ifv!(want & mask >= 1, 1, 0);
                if flag != a0 ^ a1 {
                    // debug!("ng", i, flag, a0, a1, k0, k1);
                    continue;
                }
                // debug!("ok", i, flag, a0, a1, k0, k1);

                // zero
                dp[i+1][nk0][nk1] = min(dp[i+1][nk0][nk1], base);

                // one
                let nk0 = ifv!(a0 == 0, 1, nk0);
                let nk1 = ifv!(a1 == 1, 1, nk1);
                dp[i+1][nk0][nk1] = min(dp[i+1][nk0][nk1], base+mask);
            }
        }
    }

    let mut best = MAX;
    for w in 0..2 {
        let ans = dp[hidx+1][0][w];
        if ans < a[0] && (a[0]-ans) ^ (a[1]+ans) == want {
            best = min(best, ans);
        }
    }
    println!("{}", ifv!(best == MAX, -1, best));
}


Measured memory_bytes: 2196