#include <vector>
#include <stack>
#include <queue>
#include <list>
#include <bitset>
#include <set>
#include <map>
#include <unordered_set>
#include <unordered_map>
#include <algorithm>
#include <numeric>
#include <iostream>
#include <iomanip>
#include <string>
#include <chrono>
#include <random>
#include <cmath>
#include <cassert>
#include <climits>
#include <cstring>
#include <cstdlib>
#include <functional>
#include <sstream>

using namespace std;


int main(int argc, char** argv) {
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout << fixed << setprecision(12);

    int n;
    cin >> n;
    vector<long long> A(n);
    for (int i = 0; i < n; ++i) {
        cin >> A[i];
    }

    auto calc = [&](const vector<long long>& A, int beg = 0) {
        long long res = 0;
        int n = A.size();
        for (int i = beg; i < n; ++i) {
            res ^= A[i];
        }
        return res;
    };

    auto toBin = [&](long long x) {
        if (x == 0) {
            return string(1, '0');
        }
        bitset<60> bits(x);
        string res = bits.to_string();

        reverse(res.begin(), res.end());
        while (!res.empty() && res.back() == '0') {
            res.pop_back();
        }
        if (res.empty()) {
            res = "0";
        }
        reverse(res.begin(), res.end());
        return res;
    };

    long long total = calc(A);
    // cout << total << endl;
    if (total == 0) {
        cout << 0 << '\n';
    } else {
        long long tar = calc(A, 2);
        // cout << "TAR " << tar << endl;

        long long a = A[0];
        long long b = A[1];

        
        string S = toBin(a);
        string T = toBin(b);
        string D = toBin(tar);

        int N = max(max(S.length(), T.length()), D.length());
        if (S.length() < N) {
            S = string(N - S.length(), '0') + S;
        }
        if (T.length() < N) {
            T = string(N - T.length(), '0') + T;
        }
        if (D.length() < N) {
            D = string(N - D.length(), '0') + D;
        }
        // cout << S << " " << T << " " << D << " " << endl;
        const long long INF = 1LL << 60;
        int M = 1 << 2;
        vector<vector<long long>> dp(N + 1, vector<long long>(M, INF));
        dp[N][0] = 0;


        for (int i = N - 1, BIT = 0; i >= 0; --i, ++BIT) {
            for (int j = 0; j < M; ++j) {
                if (dp[i + 1][j] >= INF) {
                    continue;
                }
                int a = S[i] - '0';
                int b = T[i] - '0';
                int c = D[i] - '0';
                // cout << i << " " << j << " : " << BIT << " : " << a << " " << b << " " << c << "  " << " : " << dp[i + 1][j] << endl;

                for (int k = 0; k < 2; ++k) {
                    int na = a + k * 2 - (j & 1);
                    int nb = b + (j >> 1); 
                    // cout << k << " " << na << " " << nb << endl;
                    if (((na ^ nb) & 1) == c && (na & 2) == 0) {
                        int nj = 0;
                        nj |= k;
                        if (nb & 2) {
                            nj |= 2;
                        }
                        dp[i][nj] = min(dp[i][nj], dp[i + 1][j]);
                        // cout << "nj " << nj << " " << dp[i][nj] << endl;
                    }
                    if (na - 1 >= 0 && (((na - 1) ^ (nb + 1)) & 1) == c && ((na - 1) & 2) == 0){
                        int nj = 0;
                        nj |= k;
                        if ((nb + 1) & 2) {
                            nj |= 2;
                        }
                        
                        dp[i][nj] = min(dp[i][nj], dp[i + 1][j] + (1LL << BIT));
                        // cout << "nj1 " << nj << " " << dp[i][nj] << endl;
                    }
                }
            }
            // cout << endl;
        }

        long long res = dp[0][0];
        if (res >= A[0] || res < 0) {
            cout << -1 << '\n';
        } else {
            cout << res << '\n';
        }
    }

    return 0;
}

Measured memory_bytes: 3672