#pragma GCC optimize("Ofast")
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
using ll = long long;
using u64 = uint_fast64_t;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
#define rep(i, n) for(int i = 0; i < (n); ++i)
#define all(x) (x).begin(),(x).end()
constexpr char ln =  '\n';
constexpr long long MOD = 1000000007;
//constexpr long long MOD = 998244353;
template<class T1, class T2> inline bool chmax(T1 &a, T2 b) { if (a < b) { a = b; return true;} return false; }
template<class T1, class T2> inline bool chmin(T1 &a, T2 b) { if (a > b) { a = b; return true;} return false; }
inline int popcount(int x) {return __builtin_popcount(x);}
inline int popcount(long long x) {return __builtin_popcountll(x);}
void print() { cout << "\n"; }
template<class T, class... Args>
void print(const T &x, const Args &... args) {
    cout << x << " ";
    print(args...);
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////

ll dp[60][2][2];

int main() {
    ios::sync_with_stdio(false); cin.tie(nullptr);
    int N; cin >> N;
    vector<ll> A(N);
    rep(i,N) cin >> A[i];

    ll X = 0;
    for (int i = 2; i < N; i++) X ^= A[i];

    
    rep(i,60) rep(j,2) rep(k,2) dp[i][j][k] = 1e18;
    //i個のbitを下から揃えて繰り下がっていない/いる、k番目が繰り上がっている/いないときの最小値
    dp[0][0][0] = 0;
    rep(bit,59) {
        int u = (A[0]>>bit)&1;
        int v = (A[1]>>bit)&1;
        int w = (X>>bit)&1;
        rep(i,2) {
            rep(j,2) {
                if (dp[bit][i][j]==1e18) continue;
                int uu = i ? 1-u : u;
                int vv = j ? 1-v : v;
                if ((uu^vv)!=w) continue;
                if (u==0 and v==0 and i==0 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][0][0]);
                    chmin(dp[bit+1][1][0],dp[bit][0][0]+(1LL<<bit)); 
                }
                if (u==0 and v==1 and i==0 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][0][0]);
                    chmin(dp[bit+1][1][1],dp[bit][0][0]+(1LL<<bit));
                }
                if (u==1 and v==0 and i==0 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][0][0]);
                    chmin(dp[bit+1][0][0],dp[bit][0][0]+(1LL<<bit));
                }
                if (u==1 and v==1 and i==0 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][0][0]);
                    chmin(dp[bit+1][0][1],dp[bit][0][0]+(1LL<<bit));
                }

                if (u==0 and v==0 and i==0 and j==1) {
                    chmin(dp[bit+1][0][0],dp[bit][0][1]);
                    chmin(dp[bit+1][1][1],dp[bit][0][1]+(1LL<<bit)); 
                }
                if (u==0 and v==1 and i==0 and j==1) {
                    chmin(dp[bit+1][0][1],dp[bit][0][1]);
                    chmin(dp[bit+1][1][1],dp[bit][0][1]+(1LL<<bit));
                }
                if (u==1 and v==0 and i==0 and j==1) {
                    chmin(dp[bit+1][0][0],dp[bit][0][1]);
                    chmin(dp[bit+1][0][1],dp[bit][0][1]+(1LL<<bit));
                }
                if (u==1 and v==1 and i==0 and j==1) {
                    chmin(dp[bit+1][0][1],dp[bit][0][1]);
                    chmin(dp[bit+1][0][1],dp[bit][0][1]+(1LL<<bit));
                }

                if (u==0 and v==0 and i==1 and j==0) {
                    chmin(dp[bit+1][1][0],dp[bit][1][0]);
                    chmin(dp[bit+1][1][0],dp[bit][1][0]+(1LL<<bit)); 
                }
                if (u==0 and v==1 and i==1 and j==0) {
                    chmin(dp[bit+1][1][0],dp[bit][1][0]);
                    chmin(dp[bit+1][1][1],dp[bit][1][0]+(1LL<<bit));
                }
                if (u==1 and v==0 and i==1 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][1][0]);
                    chmin(dp[bit+1][1][0],dp[bit][1][0]+(1LL<<bit));
                }
                if (u==1 and v==1 and i==1 and j==0) {
                    chmin(dp[bit+1][0][0],dp[bit][1][0]);
                    chmin(dp[bit+1][1][1],dp[bit][1][0]+(1LL<<bit));
                }

                if (u==0 and v==0 and i==1 and j==1) {
                    chmin(dp[bit+1][1][0],dp[bit][1][1]);
                    chmin(dp[bit+1][1][1],dp[bit][1][1]+(1LL<<bit)); 
                }
                if (u==0 and v==1 and i==1 and j==1) {
                    chmin(dp[bit+1][1][1],dp[bit][1][1]);
                    chmin(dp[bit+1][1][1],dp[bit][1][1]+(1LL<<bit));
                }
                if (u==1 and v==0 and i==1 and j==1) {
                    chmin(dp[bit+1][0][0],dp[bit][1][1]);
                    chmin(dp[bit+1][1][1],dp[bit][1][1]+(1LL<<bit));
                }
                if (u==1 and v==1 and i==1 and j==1) {
                    chmin(dp[bit+1][0][1],dp[bit][1][1]);
                    chmin(dp[bit+1][1][1],dp[bit][1][1]+(1LL<<bit));
                }
            }
        }
    }

    cout << (dp[59][0][0] >= A[0] ? -1 : dp[59][0][0]) << ln;
}


Measured memory_bytes: 3624