#include <iostream>
#include <sstream>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <stack>
#include <memory>
#include <complex>
#include <numeric>
#include <cstdio>
#include <iomanip>
#include <random>

#define REP(i,m,n) for(int i=int(m);i<int(n);i++)
#define RREP(i,m,n) for(int i=int(n)-1;i>=int(m);--i)
#define EACH(i,c) for (auto &(i): c)
#define all(c) begin(c),end(c)
#define EXIST(s,e) ((s).find(e)!=(s).end())
#define SORT(c) sort(begin(c),end(c))
#define pb emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
//#define int long long

#ifdef LOCAL
#define DEBUG(s) cout << (s) << endl
#define dump(x)  cerr << #x << " = " << (x) << endl
#define BR cout << endl;
#else
#define DEBUG(s) do{}while(0)
#define dump(x) do{}while(0)
#define BR 
#endif
using namespace std;

using UI = unsigned int;
using UL = unsigned long;
using LL = long long;
using ULL = unsigned long long;
using VI = vector<int>;
using VVI = vector<VI>;
using VLL = vector<LL>;
using VVLL = vector<VLL>;
using VS = vector<string>;
using PII = pair<int,int>;
using VP = vector<PII>;
template<class T>bool chmax(T &a, const T &b) { if (a<b) { a=b; return 1; } return 0; }
template<class T>bool chmin(T &a, const T &b) { if (b<a) { a=b; return 1; } return 0; }

int countbits(long long bits)
{
    bits = (bits & 0x55555555) + (bits >> 1 & 0x55555555);
    bits = (bits & 0x33333333) + (bits >> 2 & 0x33333333);
    bits = (bits & 0x0f0f0f0f) + (bits >> 4 & 0x0f0f0f0f);
    bits = (bits & 0x00ff00ff) + (bits >> 8 & 0x00ff00ff);
    return (bits & 0x0000ffff) + (bits >>16 & 0x0000ffff);
}

void solve() {
    int n;
    cin >> n;
    VLL a(n);
    REP(i,0,n) cin >> a[i];
    /*
    if (n == 2) {
        if ((a[0] + a[1]) % 2 == 0 && a[0] > a[1]) {
            cout << (a[0] - a[1]) / 2 << endl;
        } else {
            cout << -1 << endl;
        }
        return;
    }
    */

    LL x = 0;
    REP(i,2,n) x ^= a[i];
    /*
    if (countbits(x ^ a[0] ^ a[1]) == 0) {
        cout << 0 << endl;
        return;
    }
    */
    LL l = 0, r = 0;
    dump(x);
    REP(i,0,60) {
        if (x & (1LL<<i)) {
            r += (1LL<< i);
        }
    }
    LL cur = l + r;
    LL rem = a[0] + a[1] - cur;
    dump(cur);
    dump(rem);
    dump(l);
    dump(r);
    if (rem < 0) {
        cout << -1 << endl;
        return;
    }
    RREP(i,0,60) {
        if ((1LL<<i)*2 <= rem) {
            l += (1LL<<i);
            r += (1LL<<i);
            rem -= (1LL<<i)*2;
        }
    }
    dump(l);
    dump(r);
    RREP(i,0,60) {
        if (l & (1LL<<i)) continue;
        if (r & (1LL<<i)) {
            if (l + (1LL<<i) <= a[0]) {
                l += (1LL<<i);
                r -= (1LL<<i);
            }   
        }        
    }
    // REP(i,0,60) {
    //     if (l & (1LL<<i)) break;
    //     if (r & (1LL<<i)) {
    //         l += (1LL<<i);
    //         r -= (1LL<<i);
    //         break;
    //     }
    // }
    dump(l);
    dump(r);
    if (l + r != a[0] + a[1] || l > a[0] || r < a[1] || l == 0 || (l ^ r) != x) {
        cout << -1 << endl;
        return;
    }
    cout << a[0] - l << endl;
}

signed main() {
    solve();
    
    return 0;
}

Measured memory_bytes: 3604