// Standard Stuff
#include <bits/stdc++.h>
using namespace std;

// Macros
#define F  first
#define S  second
#define PB push_back

// Rename Datatypes
using ll  = long long;
using ld  = long double;
using pii = pair<int,int>;

// Common Functions
ll fastpow(ll a, ll ex){ ll res = 1; while(ex){ if(ex & 1) res *= a; a *= a; ex >>= 1; } return res; }
ll fastpow(ll a, ll ex, ll m){ a %= m; ll res = 1; while(ex){ if(ex & 1) res = (a*res) % m; a = (a*a) % m; ex >>= 1; } return res; }
ll modinverse(ll a, ll m){ ll m0 = m, y = 0, x = 1; while (a > 1){ ll q = a / m; a %= m; swap(a,m); x -= q*y; swap(x,y); } if(x < 0) x += m0; return x; }

// HELPERS
//  Tedious Grid Movements
vector<pii> moves_grid = { {0,1} , {0,-1} , {1,0} , {-1,0} };
inline bool move_okay(int x, int y, int n, int m) { return 0 <= x and x < n and 0 <= y and y < m; }
//  Remember:
//   * vec.assign() GOOD; vec.resize() BAD
//   * comparator uses <

// Output, Common Containers
template <typename T> ostream& operator << (ostream &os, const vector<T> &v) { for (T i : v) os << i << ' '; return os; }
template <typename T> ostream& operator << (ostream &os, const set<T> &v)    { for (T i : v) os << i << ' '; return os; }
template <typename T, typename S> ostream& operator << (ostream &os, const pair<T,S> &v) { os << "(" << v.F << "=>" << v.S << ") "; return os; }
template <typename T, typename S> ostream& operator << (ostream &os, const map<T,S> &v)  { for (auto i : v) os << i << ' '; return os; }

// Debugging
#ifdef SANYAM1101
	#define trace(args...) do{ cerr << "TRACE ON! (Line: " << __LINE__ << ")\n"; auto _stream = istringstream{string{#args}}; d_f(_stream, args); } while(0)
	void d_f(__attribute__((unused)) istringstream& _stream) {}
	template<typename Head, typename... Tails> void d_f(istringstream& _stream, Head H, Tails... T) { string _name; _stream >> _name; if(_name.back() == ',') _name.pop_back(); cerr << "\t" << _name << " <=> [" << H << "]\n"; d_f(_stream, T...); }
#else
	#define trace(args...)
#endif


// CODE STARTS HERE

void solve(){
	int n;
	cin >> n;

	ll first, second;
	cin >> first >> second;

	ll SUM = first + second;

	ll XOR = 0;
	for(int i = 0; i < n - 2; ++i){
		ll x; cin >> x;
		XOR ^= x;
	}

	// a + b = (a ^ b) + 2 * (a & b)
	// SUM = XOR + 2 * AND
	// AND = (SUM - XOR) / 2

	if(SUM < XOR or ((SUM - XOR) & 1)){
		cout << "-1\n";
		return;
	}

	ll AND = (SUM - XOR) / 2;

	ll a = 0, b = 0;

	auto check_set_bit = [](auto x, auto pos) -> bool{
		return (x >> pos) & 1;
	};

	// Set both bits wherever AND bit is set
	for(int bit = 60; bit >= 0; --bit){
		if(check_set_bit(AND, bit)){
			// AND says both 1, XOR says different
			// Contradiction
			if(check_set_bit(XOR, bit)){
				cout << "-1\n";
				return;
			}

			ll num = 1LL << bit;
			a |= num;
			b |= num;
		}
	}

	// Greedily set bits where AND bit is not set but XOR bit is
	// (which means that exactly one of a,b have set bit), prefer a
	// (to minimize difference)
	for(int bit = 60; bit >= 0; --bit){
		if(not check_set_bit(AND, bit) and check_set_bit(XOR, bit)){
			ll num = 1LL << bit;
			if((a | num) <= first) a |= num;
			else b |= num;
		}
	}

	if(0 < a and a <= first) cout << first - a << "\n";
	else cout << "-1\n";
}

signed main (void){
	// Fast_IO
	ios::sync_with_stdio(false);
	cin.tie(nullptr);

	// Set Fixed Notation (default sucks), High Precision for Floating-Point Output
	cout << fixed << setprecision(20);

	// Multiple TCs
	int tt = 1;
	// cin >> tt;
	while(tt--) solve();
}


Measured memory_bytes: 3656