#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define F first
#define S second
#define int long long
#define ll long long
//#define int  unsigned long long
#define pb push_back
//#define double long double

using namespace std;
using namespace __gnu_pbds;
typedef  tree< int , null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2050;
const int K = 46;
const int M = 1e9;
const int mod = 1e9 + 7;
int n;
int a[N], b[N];
int solve(int x, int y, int bt){
    x = x ^ (x & (1LL << (bt + 1)));
    y = y ^ (y & (1LL << (bt + 1)));
    int b1 = (1 & (x >> bt));
    int b2 = (1 & (y >> bt));
   // cout << x << " " << y << " " << bt << " " << b1 << " " << b2 << " " << b[bt] << endl;
    if (bt == 0){
        if (x == y && x == b[bt]){
            return 0;
        } else
        if (x != y && b[bt] == 2){
            return 0;
        } else{
            return 1e14;
        }
    }
    if (b[bt] == 0){
        if (b1 == 0 && b2 == 0) return solve(x, y, bt - 1);
        if (b2 == 1) return 1e14;
        if (b1 == 1){
            int s = x - (1LL << bt) + 1;
            if (!(1 & ((s + y) >> bt))) return s + solve(x - s, y + s, bt - 1);
            else return 1e14;
        }
    } else
    if (b[bt] == 1){
        if (b1 == 1 && b2 == 1) return solve(x, y, bt - 1);
      //  cout << "!!" << endl;
        if (b1 == 0) return 1e14;
        int s = (1LL << bt) - y;
        if ((x >= s) && (1 & ((x - s) >> bt))) return s + solve(x - s, y + s, bt - 1);
        else return 1e14;
    } else
    if (b[bt] == 2){
        if (b1 == 0 && b2 == 1) return solve(x, y, bt - 1);
        if (b1 == 0 && b2 == 0){
            int s = (1LL << bt) - y;
            if (s <= x){
                return s + solve(x - s, y + s, bt - 1);
            } else{
                return 1e14;
            }
        }
        if (b1 == 1 && b2 == 1){
            int s = x - (1LL << bt) + 1;
            int v1 = 1e14;
            int v2 = 1e14;
            if (1 & ((s + y) >> bt)) v1 = s + solve(x - s, y + s, bt - 1);
           // cout << "s = " << s << " v1 = " << v1 << " " << x - s << " " << y + s << " " << bt - 1 << endl;
          //  s = (1LL << (bt + 1)) - y;
          //  if (x >= s && (1 & ((x - s) >> bt))) v2 = s + solve(x - s, 0, bt - 1);
            return v1;
        }
        if (b1 == 1 && b2 == 0){
            int v1 = solve(x, y, bt - 1);
            //cout << "v1 = " << v1 << " " << x << " " << y << endl;
            int s = x - (1LL << bt) + 1;
            int v2 = 1e14;
            if ((1 & ((s + y) >> bt)) && (s != (1 << bt))) v2 = s + solve(x - s, y + s, bt - 1);
            int v3 = 1e14;
            s = (1LL << bt) - y;
            if (x >= s && !(1 & ((x - s) >> bt)) && s != (1 << bt)) v3 = s + solve(x - s, 0, bt - 1);
            return min({v1, v2, v3});
        }
    }
}
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    //freopen("input.txt", "r", stdin);
    //freopen("output.txt", "w", stdout);
    int n;
    cin >> n;
    for (int i = 1; i <= n; i++){
         cin >> a[i];
    }
    int x = 0;
    for (int i = 3; i <= n; i++) x ^= a[i];
    int s = a[1] + a[2];
    for (int i = 0; i < K; i++){
        if ((1LL << i) & x) b[i] = 2;
        else{
            if ((1LL << (i + 1)) & (s - x)) b[i] = 1;
        }
    }
 //   for (int i = 0; i < K; i++) cout << b[i] << " ";
  //  cout << endl;
    int ans = solve(a[1], a[2], K - 1);
    if (ans >= a[1]){
        cout << -1;
    } else{
        cout << ans << endl;
      //  cout << ((a[1] - ans) ^ (a[1] + ans) ^ x) << endl;
       // cout << solve(3, 0, 1) << endl;
    }
}


Measured memory_bytes: 3608