#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0;char ch;
	while(!isdigit(ch=getchar()));x=ch-48;
	while(isdigit(ch=getchar()))x=x*10+ch-48;
	return x;
}
const int MAXN=105;
int n,ans=-1,a[MAXN];
vector<int>g;
map<vector<int>,int>mp;
inline int count(int x){return __builtin_popcount(x);}
inline void dfs1(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count(a[i+1]^y)%(1<<15)-count((a[i]^y)%(1<<15)));
		mp[g]=y;
		return;
	}
	dfs1(x+1,y),dfs1(x+1,y|(1<<x));
}
inline void dfs2(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count((a[i]>>15)^y)-count((a[i+1]>>15)^y));
		if(mp[g])ans=mp[g]|(y<<15);
		return;
	}
	dfs2(x+1,y),dfs2(x+1,y|(1<<x));
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	dfs1(0,0),dfs2(0,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0;char ch;
	while(!isdigit(ch=getchar()));x=ch-48;
	while(isdigit(ch=getchar()))x=x*10+ch-48;
	return x;
}
const int MAXN=105;
int n,ans=-1,a[MAXN];
vector<int>g;
map<vector<int>,int>mp;
inline int count(int x){return __builtin_popcount(x);}
inline void dfs1(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count((a[i+1]^y)&32767)-count((a[i]^y)&32767));
		mp[g]=y;
		return;
	}
	dfs1(x+1,y),dfs1(x+1,y|(1<<x));
}
inline void dfs2(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count((a[i]>>15)^y)-count((a[i+1]>>15)^y));
		if(mp[g].count())ans=mp[g]|(y<<15);
		return;
	}
	dfs2(x+1,y),dfs2(x+1,y|(1<<x));
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	dfs1(0,0),dfs2(0,0);
	printf("%d\n",ans);
	return 0;
}


#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int x=0;char ch;
	while(!isdigit(ch=getchar()));x=ch-48;
	while(isdigit(ch=getchar()))x=x*10+ch-48;
	return x;
}
const int MAXN=105;
int n,ans=-1,a[MAXN];
vector<int>g;
map<vector<int>,int>mp;
inline int count(int x){return __builtin_popcount(x);}
inline void dfs1(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count((a[i+1]^y)&32767)-count((a[i]^y)&32767));
		mp[g]=y;
		return;
	}
	dfs1(x+1,y),dfs1(x+1,y|(1<<x));
}
inline void dfs2(int x,int y)
{
	if(x==15)
	{
		g.clear();
		for(int i=1;i<n;i++)
			g.push_back(count((a[i]>>15)^y)-count((a[i+1]>>15)^y));
		if(mp[g])ans=mp[g]|(y<<15);
		return;
	}
	dfs2(x+1,y),dfs2(x+1,y|(1<<x));
}
int main()
{
	n=read();
	for(int i=1;i<=n;i++)a[i]=read();
	dfs1(0,0),dfs2(0,0);
	printf("%d\n",ans);
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int K = 15,N = 1 << K;
int a[105],n,a1[105],a2[105];
map<int,int> mp[N * K];int ans[N * K];
vector<int> calc(int a[],int x)
{
	vector<int> tmp(n),ret(n - 1);
	for (int i = 1;i <= n;i++) tmp[i - 1] = __builtin_popcount(a[i] ^ x);
	for (int i = 1;i < n;i++) ret[i - 1] = tmp[i] - tmp[0];
	return ret;
}
int ptr = 0;
void insert(const vector<int> &num,int vv)
{
	int cur = 0;
	for (auto &i : num)
	{
		if (!mp[cur][i]) mp[cur][i] = ++ptr;
		cur = mp[cur][i];
	}
	ans[cur] = vv;
}
int find(const vector<int> &num)
{
	int cur = 0;
	for (auto &i : num)
	{
		if (!mp[cur].count(i)) return -1;
		cur = mp[cur][i];
	}
	return ans[cur];
}
int main ()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];a1[i] = a[i] >> K;a2[i] = a[i] ^ (a1[i] << K);
	}
	for (int i = 0;i < N;i++) insert(calc(a1,i),i);
	for (int i = 0;i < N;i++)
	{
		auto tt = calc(a2,i);
		for (auto &j : tt) j *= -1;
		int tmp = find(tt);
		if (tmp != -1) {cout << ((tmp << K) ^ i) << endl;return 0;}
	}
	cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int K = 15,N = 1 << K;
int a[105],n,a1[105],a2[105];
map<int,int> mp[N * K + 5];int ans[N * K + 5];
vector<int> calc(int a[],int x)
{
	vector<int> tmp(n),ret(n - 1);
	for (int i = 1;i <= n;i++) tmp[i - 1] = __builtin_popcount(a[i] ^ x);
	for (int i = 1;i < n;i++) ret[i - 1] = tmp[i] - tmp[0];
	return ret;
}
int ptr = 0;
void insert(const vector<int> &num,int vv)
{
    int cur = 0;
    for (auto &i : num)
    {
    	if (!mp[cur][i]) mp[cur][i] = ++ptr;
    	cur = mp[cur][i];
    }
    ans[cur] = vv;
}
int find(const vector<int> &num)
{
    int cur = 0;
    for (auto &i : num)
    {
    	if (!mp[cur].count(i)) return -1;
    	cur = mp[cur][i];
    }
    return ans[cur];
}
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
    	cin >> a[i];a1[i] = a[i] >> K;a2[i] = a[i] ^ (a1[i] << K);
    }
    for (int i = 0;i < N;i++) insert(calc(a1,i),i);
    for (int i = 0;i < N;i++)
    {
    	auto tt = calc(a2,i);
    	for (auto &j : tt) j *= -1;
    	int tmp = find(tt);
    	if (tmp != -1) {cout << ((tmp << K) ^ i) << endl;return 0;}
    }
    cout << -1 << endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int K = 15,N = 1 << K;
int a[105],n,a1[105],a2[105];
map<int,int> mp[N * K * 10];int ans[N * K * 10];
vector<int> calc(int a[],int x)
{
	vector<int> tmp(n),ret(n - 1);
	for (int i = 1;i <= n;i++) tmp[i - 1] = __builtin_popcount(a[i] ^ x);
	for (int i = 1;i < n;i++) ret[i - 1] = tmp[i] - tmp[0];
	return ret;
}
int ptr = 0;
void insert(const vector<int> &num,int vv)
{
	int cur = 0;
	for (auto &i : num)
	{
		if (!mp[cur][i]) mp[cur][i] = ++ptr;
		cur = mp[cur][i];
	}
	ans[cur] = vv;
}
int find(const vector<int> &num)
{
	int cur = 0;
	for (auto &i : num)
	{
		if (!mp[cur].count(i)) return -1;
		cur = mp[cur][i];
	}
	return ans[cur];
}
int main ()
{
	ios::sync_with_stdio(false);
	cin >> n;
	for (int i = 1;i <= n;i++)
	{
		cin >> a[i];a1[i] = a[i] >> K;a2[i] = a[i] ^ (a1[i] << K);
	}
	for (int i = 0;i < N;i++) insert(calc(a1,i),i);
	for (int i = 0;i < N;i++)
	{
		auto tt = calc(a2,i);
		for (auto &j : tt) j *= -1;
		int tmp = find(tt);
		if (tmp != -1) {cout << ((tmp << K) ^ i) << endl;return 0;}
	}
	cout << -1 << endl;
	return 0;
}


#include <bits/stdc++.h>
using namespace std;
const int K = 15,N = 1 << K;
int a[105],n,a1[105],a2[105];
map<int,int> mp[N * 105];int ans[N * 105];
vector<int> calc(int a[],int x)
{
	vector<int> tmp(n),ret(n - 1);
	for (int i = 1;i <= n;i++) tmp[i - 1] = __builtin_popcount(a[i] ^ x);
	for (int i = 1;i < n;i++) ret[i - 1] = tmp[i] - tmp[0];
	return ret;
}
int ptr = 0;
void insert(const vector<int> &num,int vv)
{
    int cur = 0;
    for (auto &i : num)
    {
    	if (!mp[cur][i]) mp[cur][i] = ++ptr;
    	cur = mp[cur][i];
    }
    ans[cur] = vv;
}
int find(const vector<int> &num)
{
    int cur = 0;
    for (auto &i : num)
    {
    	if (!mp[cur].count(i)) return -1;
    	cur = mp[cur][i];
    }
    return ans[cur];
}
int main ()
{
    ios::sync_with_stdio(false);
    cin >> n;
    for (int i = 1;i <= n;i++)
    {
    	cin >> a[i];a1[i] = a[i] >> K;a2[i] = a[i] ^ (a1[i] << K);
    }
    for (int i = 0;i < N;i++) insert(calc(a1,i),i);
    for (int i = 0;i < N;i++)
    {
    	auto tt = calc(a2,i);
    	for (auto &j : tt) j *= -1;
    	int tmp = find(tt);
    	if (tmp != -1) {cout << ((tmp << K) ^ i) << endl;return 0;}
    }
    cout << -1 << endl;
    return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];

int check(int p){//求 b中1的个数
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (1 << (15)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	if (n == 1){
		cout << 0 << endl, exit(0);
	}
	//分成两部分 第一部分15位 第二部分15位

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i]-vi[i+1]
	vector<int>c;
	for (int i = 0; i <= (1 << 15)-1; i++){//小的十五位
		for (int j = 1; j <= n; j++){
			//int p = __builtin_popcount(a[j] ^ i);
			int p = check((a[j] ^ i) % (1 << 15));
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vis[i][1] - vis[i][j]);
		mp[v]++;
		if (i == 0)
			c = v;
		if ((mmp[v] == 0 && v != c) || i == 0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){//大的十五位
		for (int j = 1; j <= n; j++){
			int q = (a[j] >> 15);
			int p = check(q^i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][1]);
		if (mmp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (3 << (2)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	if (n == 1){
		cout << 0 << endl, exit(0);
	}
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]
	vector<int>c;
	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			//int p = __builtin_popcount(a[j] ^ i);
			int p = check(a[j], i);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (i == 0)
			c = v;
		if ((mmp[v] == 0 && v != c)||i==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] % (1 << 15);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (3 << (2)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	if (n == 1){
		cout << 0 << endl, exit(0);
	}
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]
	vector<int>c;
	for (int i = 0; i <= (1 << 15)-1; i++){//小的十五位
		for (int j = 1; j <= n; j++){
			//int p = __builtin_popcount(a[j] ^ i);
			int p = check(a[j], i) % (1 << 15);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (i == 0)
			c = v;
		if ((mmp[v] == 0 && v != c) || i == 0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = (a[j] >> 15);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (3 << (2)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]

	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			int p = check(a[j], i);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (mmp[v]==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] / (1 << 15);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]

	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			int p = check(a[j], i);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (mmp[v]==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] / (1 << 14);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i *= (1 << 14);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]

	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			int p = check(a[j], i);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (mmp[v]==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] / (1 << 14);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 14);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];

int check(int p){//求 b中1的个数
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (1 << (15)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	if (n == 1){
		cout << 0 << endl, exit(0);
	}
	//分成两部分 第一部分15位 第二部分15位

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i]-vi[i+1]

	for (int i = 0; i <= (1 << 15)-1; i++){//小的十五位
		for (int j = 1; j <= n; j++){
			//int p = __builtin_popcount(a[j] ^ i);
			int p = check((a[j] ^ i) % (1 << 15));
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vis[i][1] - vis[i][j]);
		mmp[v] = i + 1;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){//大的十五位
		for (int j = 1; j <= n; j++){
			int q = (a[j] >> 15);
			int p = check(q^i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][1]);
		if (mmp[v] != 0){//找到答案
			int p = mmp[v];
			i = i * (1 << 15) + mmp[v] - 1;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	//cout << (3 << (2)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	if (n == 1){
		cout << 0 << endl, exit(0);
	}
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]
	vector<int>c;
	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			//int p = __builtin_popcount(a[j] ^ i);
			int p = check(a[j], i) % (1 << 15);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (i == 0)
			c = v;
		if ((mmp[v] == 0 && v != c)||i==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] >> 15;
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


//https://codeforces.com/contest/1257/problem/F
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 100;
int n;
int a[maxn];
int b[maxn];
int vis[maxn][200];
int vi[maxn][200];
int x[maxn];//代表你选的x
char s[40], ss[40];
int g = (1 << 14);
int check(int x, int y){//求 b中1的个数
	int p = x^y;
	int cnt = 0;
	while (p){
		if (p & 1)
			cnt++;
		p /= 2;
	}
	return cnt;
}
map<vector<int>, int>mp;
map<vector<int>, int>mmp;
int main(){
	cout << (3 << (2)) << endl;
	scanf("%d", &n);
	for (int i = 1; i <= n; i++)
		scanf("%d", &a[i]);
	//分成两部分 第一部分15位 第二部分15位
// 	for (int i = 0; i <= (1 << 15)-1; i++)//15位
// 		x[i] = i;

	// vis[i]+vi[i]==vis[i+1]+vi[i+1]
	//vis[i+1]-vis[i]==vi[i+1]-vi[i]

	for (int i = 0; i <= (1 << 15)-1; i++){
		for (int j = 1; j <= n; j++){
			int p = check(a[j], i);
			vis[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++){
			v.push_back(vis[i][j] - vis[i][j - 1]);
		}
		mp[v]++;
		if (mmp[v]==0)
			mmp[v] = i;
	}
	for (int i = 0; i <= (1 << 15) - 1; i++){
		for (int j = 1; j <= n; j++){
			int q = a[j] / (1 << 15);
			int p = check(q, i);
			vi[i][j] = p;
		}
		vector<int>v;
		for (int j = 2; j <= n; j++)
			v.push_back(vi[i][j] - vi[i][j - 1]);
		if (mp[v] != 0){//找到答案
			int p = mmp[v];
			i = (i << 15);
			i += p;
			cout << i << endl, exit(0);
		}
	}
	cout << -1 << endl;
	return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define INF 1000000000
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(cont, it) \
  for (typeof((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define pb push_back
#define _                                               \
  if (argc == 2 && ((string)argv[1]) == "USE_IO_FILES") \
    freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define maxN 100
#define halfBits 15

int N, a[maxN], t[maxN << halfBits][halfBits + 1], mu[maxN << halfBits], tc,
    bc[1 << halfBits];

int check(int mask, int cn, int n, int eb) {
  if (n == N) {
    return mu[cn];
  }
  int bcc = bc[(a[n] ^ mask) >> halfBits];
  if (bcc <= eb && t[cn][eb - bcc] != -1)
    return check(mask, t[cn][eb - bcc], n + 1, eb);
  return -1;
}

int main(int argc, char *argv[]) {
  _;
  FOR(i, 0, 1 << halfBits) {
    bc[i] = 0;
    FOR(j, 0, halfBits) {
      if (i & 1 << j) bc[i]++;
    }
  }
  while (scanf("%d", &N) != EOF) {
    memset(t, -1, sizeof(t));
    memset(mu, 0, sizeof(mu));
    tc = 1;
    FOR(i, 0, N) { scanf("%d", a + i); }
    FOR(i, 0, 1 << halfBits) {
      int cn = 0;
      FOR(j, 0, N) {
        int bcc = bc[(a[j] ^ i) & (1 << halfBits) - 1];
        if (t[cn][bcc] == -1) {
          t[cn][bcc] = tc++;
        }
        cn = t[cn][bcc];
      }
      mu[cn] = i;
    }
    int ans = -1;
    FOR(i, 0, 1 << halfBits) {
      FOR(j, 0, halfBits + 1) {
        int xx = check(i << halfBits, 0, 0, j);
        if (xx != -1) {
          ans = i << halfBits ^ xx;
          break;
        }
      }
      if (ans != -1) break;
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define INF 1000000000
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(cont, it) \
  for (typeof((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define pb push_back
#define _                                               \
  if (argc == 2 && ((string)argv[1]) == "USE_IO_FILES") \
    freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define maxN 100
#define halfBits 15

int N, a[maxN], t[maxN << halfBits][halfBits + 1], mu[maxN << halfBits], tc,
    bc[1 << halfBits];

int check(int mask, int cn, int n, int eb) {
  if (n == N) {
    return mu[cn];
  }
  int bcc = bc[(a[n] ^ mask) >> halfBits];
  if (t[cn][eb - bcc] != -1) return check(mask, t[cn][eb - bcc], n + 1, eb);
  return -1;
}

int main(int argc, char *argv[]) {
  _;
  FOR(i, 0, 1 << halfBits) {
    bc[i] = 0;
    FOR(j, 0, halfBits) {
      if (i & 1 << j) bc[i]++;
    }
  }
  while (scanf("%d", &N) != EOF) {
    memset(t, -1, sizeof(t));
    memset(mu, 0, sizeof(mu));
    tc = 1;
    FOR(i, 0, N) { scanf("%d", a + i); }
    FOR(i, 0, 1 << halfBits) {
      int cn = 0;
      FOR(j, 0, N) {
        int bcc = bc[(a[j] ^ i) & (1 << halfBits) - 1];
        if (t[cn][bcc] == -1) {
          t[cn][bcc] = tc++;
        }
        cn = t[cn][bcc];
      }
      mu[cn] = i;
    }
    int ans = -1;
    FOR(i, 0, 1 << halfBits) {
      FOR(j, 0, halfBits + 1) {
        int xx = check(i << halfBits, 0, 0, j);
        if (xx != -1) {
          ans = i << halfBits ^ xx;
          break;
        }
      }
      if (ans != -1) break;
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

#define INF 1000000000
#define FOR(i, a, b) for (int i = int(a); i < int(b); i++)
#define FORC(cont, it) \
  for (typeof((cont).begin()) it = (cont).begin(); it != (cont).end(); it++)
#define pb push_back
#define _                                               \
  if (argc == 2 && ((string)argv[1]) == "USE_IO_FILES") \
    freopen("in.txt", "r", stdin), freopen("out.txt", "w", stdout);

using namespace std;

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<vi> vvi;

#define maxN 100
#define halfBits 15

int N, a[maxN], t[maxN << halfBits][halfBits + 1], mu[maxN << halfBits], tc,
    bc[1 << halfBits];

int check(int mask, int cn, int n, int eb) {
  if (n == N) {
    return mu[cn];
  }
  int bcc = bc[(a[n] ^ mask) >> halfBits];
  if (t[cn][eb - bcc] != -1) return check(mask, t[cn][eb - bcc], n + 1, eb);
  return -1;
}

int main(int argc, char *argv[]) {
  _;
  FOR(i, 0, 1 << halfBits) {
    bc[i] = 0;
    FOR(j, 0, halfBits) {
      if (i & 1 << j) bc[i]++;
    }
  }
  while (scanf("%d", &N) != EOF) {
    memset(t, -1, sizeof(t));
    memset(mu, 0, sizeof(mu));
    tc = 1;
    FOR(i, 0, N) { scanf("%d", a + i); }
    FOR(i, 0, 1 << halfBits) {
      int cn = 0;
      FOR(j, 0, N) {
        int bcc = bc[(a[j] ^ i) & (1 << halfBits) - 1];
        if (t[cn][bcc] == -1) {
          t[cn][bcc] = tc++;
        }
        cn = t[cn][bcc];
      }
      mu[cn] = i;
    }
    int ans = -1;
    FOR(i, 0, 1 << halfBits) {
      FOR(j, 0, halfBits + 1) {
        int xx = check(i, 0, 0, j);
        if (xx != -1) {
          ans = i << halfBits ^ xx;
          break;
        }
      }
      if (ans != -1) break;
    }
    printf("%d\n", ans);
  }
  return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 10100;

map <vector<int>, int> mp1;

int n, a[Maxn], b[Maxn];

int main() {
	scanf("%d", &n); int all = (1 << 15) - 1;
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] >> 15) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		mp1[t] = s;
	}
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] & all) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		if(mp1[t]) {
			printf("%d\n", (mp1[t] << 15) | s);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 10100;

map <vector<int>, int> mp1;

int n, a[Maxn], b[Maxn];

int main() {
	scanf("%d", &n); int all = (1 << 15) - 1;
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] >> 15) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i - 1] - b[i]);
		mp1[t] = s;
	}
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] & all) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i - 1] - b[i]);
		if(mp1[t]) {
			printf("%d\n", (mp1[t] << 15) | s);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 10100;

map <vector<int>, int> mp1;

int n, a[Maxn], b[Maxn];

int main() {
	scanf("%d", &n); int all = (1 << 15) - 1;
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] >> 15) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		mp1[t] = s;
//		int T = t.size();
//		for(int i = 0; i < T; ++i) cout << t[i] << ' ';
//		cout << "@" << mp1[t] << endl;
	}
//	vector <int> w; w.push_back(0); cout << mp1[w] << endl;
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] & all) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		if(mp1[t] != 0) {
			printf("%d\n", (mp1[t] << 15) + s);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 10100;

map <vector<int>, int> mp1;

int n, a[Maxn], b[Maxn];

int main() {
	scanf("%d", &n); int all = (1 << 15) - 1;
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] >> 15) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i - 1] - b[i]);
		mp1[t] = s;
	}
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] & all) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		if(mp1[t]) {
			printf("%d\n", (mp1[t] << 15) | s);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


#include <bits/stdc++.h>

using namespace std;

const int Maxn = 10100;

map <vector<int>, int> mp1;

int n, a[Maxn], b[Maxn];

int main() {
	scanf("%d", &n); int all = (1 << 15) - 1;
	for(int i = 1; i <= n; ++i) scanf("%d", &a[i]);
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] >> 15) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		if(!mp1[t]) mp1[t] = s;
	}
	for(int s = 0; s <= all; ++s) {
		vector <int> t;
		for(int i = 1; i <= n; ++i) b[i] = __builtin_popcount((a[i] & all) ^ s);
		for(int i = 2; i <= n; ++i) t.push_back(b[i] - b[i - 1]);
		if(mp1[t]) {
			printf("%d\n", (mp1[t] << 15) | s);
			return 0;
		}
	}
	puts("-1");
	return 0;
}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int N; cin >> N;
    vector<int> A(N);
    const int LIM = (1 << 30) - 1;
    const int HALF = (1 << 15) - 1;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    map<vector<int>, int> solutions;
    vector<int> hash_set;

    for (int BITS = 0; BITS <= HALF; BITS++) {
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ (A[i] >> 15)));
        }
        int mn = *min_element(hash_set.begin(), hash_set.end());
        for (int i = 0; i < N; i++) {
            hash_set[i] = hash_set[i] - mn;
        }
        solutions[hash_set] = BITS;
    }

    for (int BITS = 0; BITS <= HALF; BITS++) {
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ (A[i] ^ ((A[i] >> 15) << 15))));
        }

        int mx = *max_element(hash_set.begin(), hash_set.end());
        for (int i = 0; i < N; i++) {
            hash_set[i] = mx - hash_set[i];
        }

        if (solutions.count(hash_set) == 0) continue;
        cout << ((BITS << 15) | solutions[hash_set]) << "\n";
        return 0;

    }

    cout << -1 << "\n";
    return 0;

}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int N; cin >> N;
    vector<int> A(N);
    const int LIM = (1 << 30) - 1;
    const int HALF = (1 << 15) - 1;
    for (int i = 0; i < N; i++) {
        cin >> A[i];
    }

    map<vector<int>, int> solutions;
    vector<int> hash_set;

    for (int BITS = 0; BITS <= HALF; BITS++) { // 15 high
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ (A[i] >> 15)));
        }
        int mn = *min_element(hash_set.begin(), hash_set.end());
        for (int i = 0; i < N; i++) {
            hash_set[i] = hash_set[i] - mn;
        }
        solutions[hash_set] = BITS;
    }

    for (int BITS = 0; BITS <= HALF; BITS++) { // 15 low
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ (A[i] & HALF)));
        }

        int mx = *max_element(hash_set.begin(), hash_set.end());
        for (int i = 0; i < N; i++) {
            hash_set[i] = mx - hash_set[i];
        }

        if (solutions.count(hash_set) == 0) continue;
        cout << ((solutions[hash_set] << 15) | BITS) << "\n";
        return 0;

    }

    cout << -1 << "\n";
    return 0;

}


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

int main() {
    ios::sync_with_stdio(0);
    cin.tie(0), cout.tie(0);

    int N; cin >> N;
    vector<int> A(N);
    const int LIM = (1 << 30) - 1;

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

    map<vector<int>, int> solutions;
    vector<int> hash_set, try_sol;

    for (int BITS = 0; BITS <= LIM / 2; BITS++) {
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ A[i]));
        }
        solutions.emplace(hash_set, BITS);
    }

    for (int BITS = LIM / 2 + 1; BITS <= LIM; BITS++) {
        hash_set.clear();
        for (int i = 0; i < N; i++) {
            hash_set.emplace_back(__builtin_popcount(BITS ^ A[i]));
        }

        for (int j = 0; j <= 30; j++) {
            try_sol.clear();
            for (int i = 0; i < N; i++) {
                try_sol.emplace_back(j - hash_set[i]);
            }

            if (solutions.find(try_sol) != solutions.end()) {
                cout << BITS + solutions[try_sol] << "\n";
                return 0;
            }
        }
    }

    cout << -1 << "\n";
    return 0;

}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 105;
map<ull, int>mp;
ull hash_(vector<int> & vec) {
	ull ans = 0;
	for (int i = 0; i < vec.size(); i++) ans = (ans * 2333 + vec[i]+200);
	return ans;
}
int n, a[maxn];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < (1 << 15); i++) {
		vector<int> vec;
		for (int j = 1; j <= n; j++) vec.push_back(__builtin_popcount((a[j] >> 15) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		mp[hash_(vec)] = i;
	}
	int all = (1 << 15) - 1;
	for (int i = 0; i < (1 << 15); i++) {
		vector<int>vec;
		for (int j = 1; j <= n; j++) vec.push_back(15 - __builtin_popcount((a[j] & all) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		ull val = hash_(vec);
		if (mp.count(val)) {
			int left = mp[val];
			return printf("%d\n", left << 15 | i), 0;
		}
	}
	printf("-1\n");
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define F(i, a, b) for (int i = int(a); i <= int(b); ++i)
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);
int a[110],n;
int solve(){
	int lim=1<<30;--lim;
	F(i,0,lim){
		int ans=__builtin_popcount(a[1]^i);bool fla=1;
		F(j,1,n)if(__builtin_popcount(a[j]^i)!=ans){fla=0;break;}
		if(fla)return i;
	}
	return -1;
}
int main()
{
	cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
	cin>>n;F(i,1,n)cin>>a[i];
	sort(a+1,a+1+n);n=unique(a+1,a+1+n)-(a+1);
	cout<<solve()<<'\n';
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 105;
map<ull, int>mp;
ull hash_(vector<int> & vec) {
	ull ans = 0;
	for (int i = 0; i < vec.size(); i++) ans = (ans * 2333 + vec[i]);
	return ans;
}
int n, a[maxn];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < (1 << 15); i++) {
		vector<int> vec;
		for (int j = 1; j <= n; j++) vec.push_back(__builtin_popcount((a[j] >> 15) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		mp[hash_(vec)] = i;
	}
	int all = (1 << 15) - 1;
	for (int i = 0; i < (1 << 15); i++) {
		vector<int>vec;
		for (int j = 1; j <= n; j++) vec.push_back(15 - __builtin_popcount((a[j] & all) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		ull val = hash_(vec);
		if (mp.count(val)) {
			int left = mp[val];
			return printf("%d\n", left << 15 | i), 0;
		}
	}
	printf("-1\n");
}


#include<bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 105;
map<ull, int>mp;
unsigned int hash_(vector<int> & vec) {
	ull ans = 0;
	for (int i = 0; i < vec.size(); i++) ans = (ans * 2333 + vec[i]+200);
	return ans;
}
int n, a[maxn];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < (1 << 15); i++) {
		vector<int> vec;
		for (int j = 1; j <= n; j++) vec.push_back(__builtin_popcount((a[j] >> 15) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		mp[hash_(vec)] = i;
	}
	int all = (1 << 15) - 1;
	for (int i = 0; i < (1 << 15); i++) {
		vector<int>vec;
		for (int j = 1; j <= n; j++) vec.push_back(15 - __builtin_popcount((a[j] & all) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		unsigned val = hash_(vec);
		if (mp.count(val)) {
			int left = mp[val];
			return printf("%d\n", left << 15 | i), 0;
		}
	}
	printf("-1\n");
}


#include<bits/stdc++.h>
#include<unordered_map>
#define F(i,a,b) for(int i=(a);i<=int(b);++i)
using namespace std;
typedef long long ll;
const int maxn = 110;
const int inf = 0x3f3f3f3f;
#define endl '\n'
vector<int>cnt;
struct cmp
{
	bool operator()(const vector<char>&A, const vector<char>&B)
	{
		F(i, 0, A.size() - 1)if (A[i] != B[i])return A[i] < B[i];
	};
};
map<vector<char>, int>ma;

int n, a[maxn][30];
int po[maxn];
void init()
{
	po[0] = 1;
	F(i, 1, 30)po[i] = po[i - 1] << 1;
}
int ans = -1;
bool dfs(int pos,int lim,int val)
{
	if (pos > lim)
	{
		if (lim == 14)
		{
			vector<char>tmp;
			F(i, 2, n)tmp.push_back(cnt[i] - cnt[1]);
			if (val == 0)
				val = val;
			if(ma.find(tmp)==ma.end())ma[tmp] = val;
			return 0;
		}
		else
		{
			vector<char>tmp;
			F(i, 2, n)tmp.push_back(-(cnt[i] - cnt[1]));
			if (val == 0)
				val = val;
			if (ma.find(tmp) == ma.end())return 0;
			else
			{
				ans = val | ma[tmp];
				return 1;
			}
		}
	}

	F(j, 0, 1)
	{
		F(i, 1, n)if (j^a[i][pos])++cnt[i];
		if (dfs(pos + 1, lim, val + (j ? po[pos] : 0)))
			return 1;
		F(i, 1, n)if (j^a[i][pos])--cnt[i];
	}
	return 0;
}
int main()
{
#ifndef endl
	freopen("C:\\Users\\VULCAN\\Desktop\\data.in", "r", stdin);
	cout << "************************************Local Test*********************************" << endl;
#endif // !endl
	ios::sync_with_stdio(false);
	int T(1), cas(0);
	while (T--)
	{
		init();
		cin >> n; cnt.resize(n + 1);
		F(i, 1, n)
		{
			cin >> a[i][0];
			F(j, 1, 30)a[i][j] = a[i][j - 1] >> 1;
			F(j, 0, 30)a[i][j] = a[i][j] & 1;
		}
		dfs(0, 14,0);
		dfs(15, 29,0);
		cout << ans << '\n';
		//cout << clock()*1. / CLOCKS_PER_SEC << '\n';
	}
	return 0;
}
//What to Debug
/*
-1.最好把全部warning都X掉,否则:https://vjudge.net/solution/19887176
0.看看自己是否有可能需要快读，禁endl
1.数组越界，爆int,浮点精度(查看精度是否达到题目要求,看有没有浮点数比较:eps)，取模操作，初始化数组，边缘数据,输出格式(cas),强制在线是否更新了las
2.通读代码，代码无逻辑错误
3.读题，找到题意理解失误或算法错误
4.放弃
*/


#include<bits/stdc++.h>
using namespace std;
const int maxn = 105;
map<unsigned int, int>mp;
unsigned int hash_(vector<int> & vec) {
	unsigned int ans = 0;
	for (int i = 0; i < vec.size(); i++) ans = (ans * 2333 + vec[i]+200);
	return ans;
}
int n, a[maxn];
int main() {
	scanf("%d", &n);
	for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
	for (int i = 0; i < (1 << 15); i++) {
		vector<int> vec;
		for (int j = 1; j <= n; j++) vec.push_back(__builtin_popcount((a[j] >> 15) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		mp[hash_(vec)] = i;
	}
	int all = (1 << 15) - 1;
	for (int i = 0; i < (1 << 15); i++) {
		vector<int>vec;
		for (int j = 1; j <= n; j++) vec.push_back(15 - __builtin_popcount((a[j] & all) ^ i));
		int minn = *min_element(vec.begin(), vec.end());
		for (int j = 0; j < vec.size(); j++) vec[j] -= minn;
		unsigned val = hash_(vec);
		if (mp.count(val)) {
			int left = mp[val];
			return printf("%d\n", left << 15 | i), 0;
		}
	}
	printf("-1\n");
}


#pragma optimization_level 3
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#include<bits/stdc++.h>
#define F(i, a, b) for (int i = int(a); i <= int(b); ++i)
using namespace std;
mt19937 rnd(chrono::steady_clock::now().time_since_epoch().count()); //uid<int> u1(5, 10); u1(rnd);
int a[110],n;
int solve(){
	int lim=1<<30;--lim;
	F(i,0,lim){
		int ans=__builtin_popcount(a[1]^i);bool fla=1;
		F(j,1,n)if(__builtin_popcount(a[j]^i)!=ans){fla=0;break;}
		if(fla)return i;
	}
	return -1;
}
int main()
{
	cin.tie(0);cout.tie(0);ios::sync_with_stdio(0);
	cin>>n;F(i,1,n)cin>>a[i];
	cout<<solve()<<'\n';
}


#include <map>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 105
#define LL long long
#define Pa pair<LL, LL>
#define Mod 998244353
#define Int register int
#define INF 1000000000000000000ll
using namespace std;
inline LL Min(LL x,LL y)
{
	if (x < y)
		return x;
	return y;
}
inline LL Max(LL x,LL y)
{
	if (x > y)
		return x;
	return y;
}
inline void read(LL &x)
{
	x = 0;
	LL f = 1;
	char s = getchar();
	while (s < '0' || s > '9')
	{
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9')
	{
		x = (x << 3) + (x << 1) + (s ^ 48);
		s = getchar();
	}
	x *= f;
}
inline void Swap(LL &x,LL &y)
{
	LL Temp = x;
	x = y;
	y = Temp;
}
LL a[MAXN], n, Ans;
vector<LL> Fren;
map<vector<LL>, LL> Map;
void Dfs(LL Num,LL Get,LL Type)
{
	if (Num == 16)
	{
		Fren.clear();
		for (Int i = 1; i <= n; ++ i)
			if (! Type)
				Fren.push_back(__builtin_popcount(Get ^ a[i]));
			else Fren.push_back(__builtin_popcount(Get ^ (a[i] >> 15)));
		for (Int i = 1; i < n; ++ i)
			Fren[i - 1] = Fren[i - 1] - Fren[i];
		Fren.pop_back();
		if (! Type)
		{
			if (! Map[Fren])
				Map[Fren] = Get;
		}
		else
		{
			if ( Map[Fren] )
				Ans = Map[Fren] | (Get << 15); // Map[Fren] ^ x = (Get << 15)
		}
		return ;
	}
	Dfs(Num + 1, Get, Type);
	Dfs(Num + 1, Get + (1ll << Num - 1), Type);
}
int main()
{
	read( n );
	for (Int i = 1; i <= n; ++ i)
		read( a[i] );
	Dfs(1, 0, 0);
	Dfs(1, 0, 1);
	printf("%lld", Ans);
	return 0;
}


#include <map>
#include <bitset>
#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm>
#define MAXN 105
#define LL long long
#define Pa pair<LL, LL>
#define Mod 998244353
#define Int register int
#define INF 1000000000000000000ll
using namespace std;
inline LL Min(LL x,LL y)
{
	if (x < y)
		return x;
	return y;
}
inline LL Max(LL x,LL y)
{
	if (x > y)
		return x;
	return y;
}
inline void read(LL &x)
{
	x = 0;
	LL f = 1;
	char s = getchar();
	while (s < '0' || s > '9')
	{
		if (s == '-')
			f = -1;
		s = getchar();
	}
	while (s >= '0' && s <= '9')
	{
		x = (x << 3) + (x << 1) + (s ^ 48);
		s = getchar();
	}
	x *= f;
}
inline void Swap(LL &x,LL &y)
{
	LL Temp = x;
	x = y;
	y = Temp;
}
LL a[MAXN], n, Ans;
vector<LL> Fren;
map<vector<LL>, LL> Map;
void Dfs(LL Num,LL Get,LL Type)
{
	if (Num == 16)
	{
		Fren.clear();
		for (Int i = 1; i <= n; ++ i)
			if (! Type)
				Fren.push_back(__builtin_popcount(Get ^ a[i]));
			else Fren.push_back(__builtin_popcount(Get ^ (a[i] >> 15)));
		for (Int i = 1; i < n; ++ i)
			Fren[i - 1] = Fren[i] - Fren[i - 1];
		Fren.pop_back();
		if (! Type)
		{
			if (! Map[Fren])
				Map[Fren] = Get;
		}
		else
		{
			if ( Map[Fren] )
				Ans = Map[Fren] | (Get << 15); // Map[Fren] ^ x = (Get << 15)
		}
		return ;
	}
	Dfs(Num + 1, Get, Type);
	Dfs(Num + 1, Get + (1ll << Num - 1), Type);
}
int main()
{
	read( n );
	for (Int i = 1; i <= n; ++ i)
		read( a[i] );
	Dfs(1, 0, 0);
	Dfs(1, 0, 1);
	printf("%lld", Ans);
	return 0;
}


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

const int maxn = 101;

int a[ maxn ];
int n;

map< vector< int >, int > m;

int Cal( int val, int s )
{
    int sum = 0;
    for( int i = 14; i >= 0; i -- )
    {
        if( ( ( val >> ( i + 15 ) & 1 ) ) ^ ( ( s >> i & 1 ) ) ) sum ++;
    }
    return sum;
}

int Cal1( int val, int s )
{
    int sum = 0;
    for( int i = 14; i >= 0; i -- )
    {
        if( ( ( val >> i & 1 ) ) ^ ( ( s >> i & 1 ) ) ) sum ++;
    }
    return sum;
}

int main()
{
    cin >> n;
    for( int i = 1; i <= n; i ++ ) cin >> a[ i ];

    for( int i = 0; i < ( 1 << 15 ); i ++ )
    {
        vector< int > v;
        for( int j = 1; j <= n; j ++ ) v.push_back( Cal( a[ j ], i ) );
        m[ v ] = i;
    }

    int goal = -1;

    for( int i = 0; i < ( 1 << 15 ); i ++ )
    {
        vector< int > v;
        for( int j = 1; j <= n; j ++ )
        {
            v.push_back( Cal1( a[ j ], i ) );
        }
        for( int j =  0; j <= 30; j ++ )
        {
            vector< int > tmp;
            for( auto x: v ) tmp.push_back( j - x );
            if( m.count( tmp ) )
            {
                int l = m[ v ];
                int r = i;
                for( int k = 0; k < 15; k ++ )
                {
                    if( r >> k & 1 ) l += ( 1 << ( k + 15 ) );
                }
                goal = l;
                break;
            }
        }
        if( goal != -1 ) break;
    }
    cout << goal << endl;
    return 0;
}


#include<bits/stdc++.h>
#define o 1000005
#define ll long long
#define FOR(i,a,b) for(int i=a;i<=b;i++)
#define REP(i,a,b) for(int i=a;i>=b;i--)
#define pa pair<ull,int>
#define mpp make_pair
#define s1 first
#define s2 second
#define ull unsigned long long
using namespace std;
inline int read()
{
	register int data=0,w=1;
	char ch=0;
	while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
	if(ch=='-')w=-1,ch=getchar();
	while(ch>='0'&&ch<='9')data=(data<<1)+(data<<3)+(ch^48),ch=getchar();
	return data*w;
}
int n,a[o],size[o],d[o],p2[o],sp2;
const ull base=1313131313131313131,base2=13131;
const int mod=479*(1<<21)+1;
ull p[o],sp;
const int M=(1<<15)-1;
map<pa,int> mp;
int main()
{
	n=read();
	p[0]=1;p2[0]=1;
	FOR(i,1,n)p[i]=p[i-1]*base,p2[i]=1ll*p2[i]*base2%mod,sp+=p[i],sp2=(sp2+p2[i])%mod;
	FOR(i,1,n)a[i]=read();
	size[0]=0;
	FOR(i,1,M)size[i]=size[i-(i&-i)]+1;
	FOR(i,0,M)
	{
		ull ha=0;int ha2=0;
		FOR(j,1,n)ha+=p[j]*size[a[j]>>15^i],ha2=(ha2+1ll*p2[j]*size[a[j]>>15^i])%mod;
		mp[mpp(ha,ha2)]=i;
	}
	FOR(i,0,M)
	{
		int Max=0;
		FOR(j,1,n)d[j]=size[a[j]&M^i],Max=max(Max,d[j]);
		ull ha=0;int ha2=0;
		FOR(j,1,n)ha+=p[j]*(Max-d[j]),ha2=(ha2+1ll*p2[j]*(Max-d[j]))%mod;
		FOR(j,Max,30)
		{
			if(mp.count(mpp(ha,ha2)))return cout<<(mp[mpp(ha,ha2)]<<15|i)<<'\n',0;
			ha+=sp;
		}
	}
	puts("-1");
	return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }
//    b[0] = 1;
//    b[1] = 1;
//    cerr << mp[b] << endl;
    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
        }
    }
    cout << -1;
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }
//    b[0] = 1;
//    b[1] = 1;
//    cerr << mp[b] << endl;
    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
            break;
        }
    }
    cout << -1;
    return 0;
}


//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }
//    b[0] = 1;
//    b[1] = 1;
//    cerr << mp[b] << endl;
    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
        }
    }
    cout << -1;
    return 0;
}


//#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }
//    b[0] = 1;
//    b[1] = 1;
//    cerr << mp[b] << endl;
    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
        }
    }
    cout << -1;
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }

    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
        }
    }
    cout << -1;
    return 0;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx")

#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>

#define pb push_back
#define F first
#define S second
#define ll long long
#define ull unsigned long long
#define ld long double
#define sqr(x) (x) * (x)
//#define all(a) a.begin(), a.end()

using namespace std;
//using namespace __gnu_pbds;

const int inf = 1e9 + 1232;

int arr[101];

int main()
{
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // LOCAL
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
//
    int n;
    cin >> n;
    vector <int> a(n);
    vector <int> b(n);
    int kek = (1 << 15) - 1;
    for(int i = 0; i < n; ++i)
    {
        cin >> arr[i];
        a[i] = (arr[i] & kek);
    }
    map <vector <int>, int> mp;
    for(int i = 0; i <= kek; ++i)
    {
        for(int j = 0; j < n; ++j)
            b[j] = __builtin_popcount(a[j] ^ i);
        mp[b] = i;
    }

    for(int i = 0; i < n; ++i)
        a[i] = (arr[i] >> 15);
    for(int i = 0; i <= kek; ++i)
    {
        int mx = 0;
        for(int j = 0; j < n; ++j)
        {
            b[j] = __builtin_popcount(a[j] ^ i);
            mx = max(mx, b[j]);
        }
        for(int j = 0; j < n; ++j)
            b[j] = mx - b[j];
        for(mx; mx <= 30; ++mx)
        {
            if (mp.count(b) == 1)
            {
                cout <<  (i << 15) + mp[b];
                return 0;
            }
            for(int k = 0; k < n; ++k)
                b[k]++;
            break;
        }
    }
    cout << -1;
    return 0;
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(1e9 + 7)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
// const int N = 105;

// vector<int> right[1<<15];
map<vector<int>,int> M;

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        rep(i,n) cin >> v[i];

        repA(i,0,(1ll<<15)-1) {
            vector<int> right;
            for(int j : v)
                right.pb(__builtin_popcount(((1<<15)-1)&(i^j)));

            int mn = right[0];
            rep(j,n) {
                right[j] -= mn;
                right[j] *= -1;
            }
            M[right] = i;
        }

        repA(i,0,(1ll<<15)-1) {
            vector<int> left;
            for(int j : v) {
                left.pb(__builtin_popcount(i^(j>>15)));
            }
            int mn = left[0];
            rep(j,n) {
                left[j] -= mn;
            }

            if(M.find(left) != M.end()) {
                cout << ((i<<15)|M[left]) << '\n';
                return 0;
            }
        }

        cout << "-1a\n";
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(1e9 + 7)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
// const int N = 105;

// vector<int> right[1<<15];
map<vector<int>,int> M;

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        rep(i,n) cin >> v[i];

        repA(i,0,(1ll<<15)-1) {
            vector<int> right;
            for(int j : v)
                right.pb(__builtin_popcount(((1<<15)-1)&(i^j)));

            int mn = right[0];
            rep(j,n) {
                right[j] -= mn;
                right[j] *= -1;
                // if(i==2) cout << right[j] << ' ';
            }
            // if(i==2) cout << "\n";
            M[right] = i;
        }

        repA(i,0,(1ll<15)-1) {
            // int k = (1<<15) + i;
            vector<int> left;
            for(int j : v) {
                left.pb(__builtin_popcount(i^(j>>15)));
            }
            rep(j,n) {
                left[j] -= left[0];
            }
            if(M.find(left) != M.end()) {
                cout << ((i<<15)|M[left]) << '\n';
                return 0;
            }
        }

        cout << "-1\n";
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(1e9 + 7)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
// const int N = 105;

// vector<int> right[1<<15];
map<vector<int>,int> M;

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        rep(i,n) cin >> v[i];

        repA(i,0,(1ll<<15)-1) {
            vector<int> right;
            for(int j : v)
                right.pb(__builtin_popcount(((1<<15)-1)&(i^j)));

            int mn = right[0];
            rep(j,n) {
                right[j] -= mn;
                right[j] *= -1;
            }
            M[right] = i;
        }

        repA(i,0,(1ll<<15)-1) {
            vector<int> left;
            for(int j : v) {
                left.pb(__builtin_popcount(i^(j>>15)));
            }
            int mn = left[0];
            rep(j,n) {
                left[j] -= mn;
            }

            if(M.find(left) != M.end()) {
                cout << ((i<<15)|M[left]) << '\n';
                return 0;
            }
        }

        cout << "-1\n";
    }
}


#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp> // Common file
#include <ext/pb_ds/tree_policy.hpp> // Including tree_order_statistics_node_update

using namespace std;
using namespace __gnu_pbds;

//find_by_order gives kth-largest element
//order_of_key gives the number of items in the set that are strictly smaller!!

// OT.find_by_order(k-1)
//
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

#define db(x) cerr << #x << "=" << x << endl
#define db2(x, y) cerr << #x << "=" << x << "," << #y << "=" << y << endl
#define db3(x, y, z) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << endl
#define db4(x, y, z, w) cerr << #x << "=" << x << "," << #y << "=" << y << "," << #z << "=" << z << "," << #w << "=" << w << endl
#define ll long long
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define mp make_pair
#define X first
#define Y second
#define sz(x) (int)((x).size())
#define pii pair<int,int>
#define MOD (ll)(1e9 + 7)
#define rep(i, n)    for(int i = 0; i < (n); ++i)
#define repA(i, a, n)  for(int i = a; i <= (n); ++i)
#define repD(i, a, n)  for(int i = a; i >= (n); --i)
#define trav(a, x) for(auto& a : x)
typedef vector<int> vi;
#define inf (ll)(1e18)
#define double long double
#define int long long

//////////////////////
// const int N = 105;

// vector<int> right[1<<15];
map<vector<int>,int> M;

int32_t main() {

    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);

    int t = 1;
    // cin >> t;
    while(t--) {
        int n;
        cin >> n;
        vector<int> v(n);
        rep(i,n) cin >> v[i];

        repA(i,0,(1ll<<15)-1) {
            vector<int> right;
            for(int j : v)
                right.pb(__builtin_popcount(((1<<15)-1)&(i^j)));

            int mn = right[0];
            rep(j,n) {
                right[j] -= mn;
                right[j] *= -1;
            }
            M[right] = i;
        }

        repA(i,0,(1ll<15)-1) {
            // int k = (1<<15) + i;
            vector<int> left;
            for(int j : v) {
                left.pb(__builtin_popcount(i^(j>>15)));
            }
            int mn = left[0];
            rep(j,n) {
                left[j] -= mn;
            }
            if(M.find(left) != M.end()) {
                cout << ((i<<15)|M[left]) << '\n';
                return 0;
            }
        }

        cout << "-1\n";
    }
}


#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define MEM(x,y) memset(x,y,sizeof(x))
#define bug(x) cout<<"debug "#x" is "<<x<<endl;
#define FIO ios::sync_with_stdio(false);
#define ALL(x) x.begin(),x.end()
#define LOG 20
const int inf =1e9;
const int maxn =3e5+7;
const int mod = 1e9+7;
const int N=3e5+7;
int n,m;
int a[maxn];

int main(){
    FIO;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    map<vector<int>,int> hash_;
    const int mask=(1<<17)-1;
    for(int i=0;i<(1<<17);i++){
        vector<int> t(n);
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]&mask)^i);
        }
        hash_[t]=i;
    }
    for(int i=0;i<(1<<15);i++){
        vector<int> t(n),p(n);
        int mx=0;
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]>>16)^i);
            mx=max(t[j],mx);
        }
        for(int sum=mx;sum<=30;sum++){
            for(int j=0;j<n;j++){
                p[j]=sum-t[j];
            }
            if(hash_.count(p)){
                cout<<hash_[p]+(i<<16)<<endl;
                return 0;
            }
        }

    }
    cout<<"-1"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define MEM(x,y) memset(x,y,sizeof(x))
#define bug(x) cout<<"debug "#x" is "<<x<<endl;
#define FIO ios::sync_with_stdio(false);
#define ALL(x) x.begin(),x.end()
#define LOG 20
const int inf =1e9;
const int maxn =3e5+7;
const int mod = 1e9+7;
const int N=3e5+7;
int n,m;
int a[maxn];

int main(){
    FIO;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    map<vector<int>,int> hash_;
    const int mask=(1<<18)-1;
    for(int i=0;i<(1<<18);i++){
        vector<int> t(n);
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]&mask)^i);
        }
        hash_[t]=i;
    }
    for(int i=0;i<(1<<12);i++){
        vector<int> t(n),p(n);
        int mx=0;
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]>>18)^i);
            mx=max(t[j],mx);
        }
        for(int sum=mx;sum<=30;sum++){
            for(int j=0;j<n;j++){
                p[j]=sum-t[j];
            }
            if(hash_.count(p)){
                cout<<hash_[p]+(i<<18)<<endl;
                return 0;
            }
        }

    }
    cout<<"-1"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define MEM(x,y) memset(x,y,sizeof(x))
#define bug(x) cout<<"debug "#x" is "<<x<<endl;
#define FIO ios::sync_with_stdio(false);
#define ALL(x) x.begin(),x.end()
#define LOG 20
const int inf =1e9;
const int maxn =3e5+7;
const int mod = 1e9+7;
const int N=3e5+7;
int n,m;
int a[maxn];

int main(){
    FIO;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    map<vector<int>,int> hash_;
    const int mask=(1<<15)-1;
    for(int i=0;i<(1<<15);i++){
        vector<int> t(n);//,t2;
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]&mask)^i);
        }
        hash_[t]=i;
    }
    for(int i=0;i<(1<<15);i++){
        vector<int> t(n),p(n);
        int mx=0;
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]>>14)^i);
            mx=max(t[j],mx);
        }
        for(int sum=mx;sum<=30;sum++){
            for(int j=0;j<n;j++){
                p[j]=sum-t[j];
            }
            if(hash_.count(p)){
                cout<<hash_[p]+(i<<14)<<endl;
                return 0;
            }
        }

    }
    cout<<"-1"<<endl;
    return 0;
}


#include <bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define PB push_back
#define LL long long
#define pii pair<int,int>
#define MEM(x,y) memset(x,y,sizeof(x))
#define bug(x) cout<<"debug "#x" is "<<x<<endl;
#define FIO ios::sync_with_stdio(false);
#define ALL(x) x.begin(),x.end()
#define LOG 20
const int inf =1e9;
const int maxn =3e5+7;
const int mod = 1e9+7;
const int N=3e5+7;
int n,m;
int a[maxn];

int main(){
    FIO;
    cin>>n;
    for(int i=0;i<n;i++)cin>>a[i];
    map<vector<int>,int> hash_;
    const int mask=(1<<17)-1;
    for(int i=0;i<(1<<17);i++){
        vector<int> t(n);
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]&mask)^i);
        }
        hash_[t]=i;
    }
    for(int i=0;i<(1<<16);i++){
        vector<int> t(n),p(n);
        int mx=0;
        for(int j=0;j<n;j++){
            t[j]=__builtin_popcount((a[j]>>16)^i);
            mx=max(t[j],mx);
        }
        for(int sum=mx;sum<=30;sum++){
            for(int j=0;j<n;j++){
                p[j]=sum-t[j];
            }
            if(hash_.count(p)){
                cout<<hash_[p]+(i<<16)<<endl;
                return 0;
            }
        }

    }
    cout<<"-1"<<endl;
    return 0;
}


#include <bits/stdc++.h>

#define maxn 200005
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> par;

ll N,A[maxn],mid=15,M1=30;
map<vector<int> , int> M;
vector<int> v;

int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for(int i=1;i<=N;i++)
        cin >> A[i];

    ll b = (1 << mid) -1;
    for(int i=0;i<=b;i++){
        v.clear();
        for(int j=1;j<=N;j++){
           int n = __builtin_popcountll((A[j] & b )^i);
           v.pb(n);
        }
       M[v]=i;
    }

    ll a = (1<< M1)- 1 - b;
    for(int k=0;k<=30;k++){
        for(int i=0;i<=b;i++){
           ll mask = i << mid;
           int no=0;
           v.clear();
           for(int j=1;j<=N;j++) {
                int n = __builtin_popcountll( (A[j] & a) ^ mask );
                if(k-n<0){
                    no=1;
                    break;
                }
                v.pb(k-n);
            }

          if(!no && M.find(v)!=M.end()){
            cout << M[v]+i << "\n";
            return 0;
          }
        }
    }

    cout << "-1\n";
}


#include <bits/stdc++.h>

#define maxn 200005
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> par;

int N,A[maxn],mid=15,M1=30;
map<vector<int> , int> M;
vector<int> v;

int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for(int i=1;i<=N;i++)
        cin >> A[i];

    int b = (1 << mid) -1;
    for(int i=0;i<=b;i++){
        v.clear();
        for(int j=1;j<=N;j++){
           int n = __builtin_popcount((A[j] & b )^i);
           v.pb(n);
        }
       M[v]=i;
    }

    int a = (1<< M1)- 1 - b;
    for(int k=0;k<=30;k++){
        for(int i=0;i<=b;i++){
           int mask = i << mid;
           v.clear();
           for(int j=1;j<=N;j++) {
                int n = __builtin_popcount( (A[j] & a) ^ mask );
                v.pb(k-n);
            }

          if(M.find(v)!=M.end()){
            cout << M[v]+mask << "\n";
            return 0;
          }
        }
    }

    cout << "-1\n";
}


#include <bits/stdc++.h>

#define maxn 200005
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> par;

ll N,A[maxn],mid=15,M1=30;
map<vector<int> , int> M;
vector<int> v;

int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for(int i=1;i<=N;i++)
        cin >> A[i];

    ll b = (1 << mid) -1;
    for(int i=0;i< (1 << mid);i++){
        v.clear();
        for(int j=1;j<=N;j++){
           int n = __builtin_popcount((A[j] & b )^i);
           v.pb(n);
        }
       M[v]=i;
    }

    ll a = (1<< M1)- 1 - b;
    for(int k=0;k<=30;k++){
        for(int i=0;i<=b;i++){
           ll mask = i << mid;
           int no=0;
           v.clear();
           for(int j=1;j<=N;j++) {
                ll n = __builtin_popcount( (A[j] & a) ^ mask );
                if(k-n<0){
                    no=1;
                    break;
                }
                v.pb(k-n);
            }

          if(!no && M.find(v)!=M.end()){
            cout << M[v]+i << "\n";
            return 0;
          }
        }
    }

    cout << "-1\n";
}


#include <bits/stdc++.h>

#define maxn 200005
#define pb push_back

using namespace std;

typedef long long ll;
typedef double ld;
typedef pair<int,int> par;

int N,A[maxn],mid=15,M1=30;
map<vector<int> , int> M;
vector<int> v;

int main()
{
    #ifdef acm
    freopen("a.in","r",stdin);
    #endif // acm
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> N;

    for(int i=1;i<=N;i++)
        cin >> A[i];

    int b = (1 << mid) -1;
    for(int i=0;i<=b;i++){
        v.clear();
        for(int j=1;j<=N;j++){
           int n = __builtin_popcount((A[j] & b )^i);
           v.pb(n);
        }
       M[v]=i;
    }

    int a = (1<< M1)- 1 - b;
    for(int k=0;k<=30;k++){
        for(int i=0;i<=b;i++){
           int mask = i << mid;
           v.clear();
           for(int j=1;j<=N;j++) {
                int n = __builtin_popcount( (A[j] & a) ^ mask );
                v.pb(k-n);
            }

          if(M.find(v)!=M.end()){
            cout << M[v]+i << "\n";
            return 0;
          }
        }
    }

    cout << "-1\n";
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i & (1 << j))) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] & (1 << j)) -- b[i][k];
                else ++ b[i][k];
                if(a[k] & (1 << j + lim)) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
    n = read();
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) {
    	int x = 0;
    	for(int j = 0; j < (lim << 1); ++ j) if(a[i] & (1 << j)) ++ x;
    	v.push_back(x);
	}
    //for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        vector <int> V = v1;
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15)), 0;
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i & (1 << j))) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] & (1 << j)) -- b[i][k];
                else ++ b[i][k];
                if(a[k] & (1 << j + lim)) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
    n = read();
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) {
    	int x = 0;
    	for(int j = 0; j < (lim << 1); ++ j) if(a[i] & (1 << j)) ++ x;
    	v.push_back(x);
	}
    //for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        vector <int> V = v1;
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15));
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i >> j & 1)) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] >> j & 1) -- b[i][k];
                else ++ b[i][k];
                if(a[k] >> (j + lim) & 1) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
    n = read();
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        vector <int> V = v1;
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15));
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i >> j & 1)) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] >> j & 1) -- b[i][k];
                else ++ b[i][k];
                if(a[k] >> (j + lim) & 1) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
	vector <int> V;
    n = read();
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        for(int j = 0; j < (lim << 1); ++ j) {
        	V = v1;
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15));
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i >> j & 1)) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] >> j & 1) -- b[i][k];
                else ++ b[i][k];
                if(a[k] >> (j + lim) & 1) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
	vector <int> V;
    n = read(); V.resize(n + 1);
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15));
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i >> j & 1)) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] >> j & 1) -- b[i][k];
                else ++ b[i][k];
                if(a[k] >> (j + lim) & 1) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
	vector <int> V;
    n = read(); V.resize(n + 1);
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15)), 0;
        }
    }
    puts("-1");
    return 0;
}


#include <map>
#include <vector>
#include <cstdio>
using namespace std;

const int N = 105, lim = 15;

int n, a[N], b[(1 << lim) + 5][N];
map <vector <int>, int> ma;

int read() {
	int x = 0, f = 1; char s;
	while((s = getchar()) > '9' || s < '0') if(s == '-') f = -1;
	while(s >= '0' && s <= '9') x = (x << 1) + (x << 3) + (s ^ 48), s = getchar();
	return x * f;
}

void init() {
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v;
        for(int j = 0; j <= n; ++ j) v.push_back(0);
        for(int j = 0; j < lim; ++ j) {
            if(! (i & (1 << j))) continue;
            for(int k = 1; k <= n; ++ k) {
                if(a[k] & (1 << j)) -- b[i][k];
                else ++ b[i][k];
                if(a[k] & (1 << j + lim)) -- v[k];
                else ++ v[k];
            }
        }
        //printf("-----------------\n");
        //for(int j = 1; j <= n; ++ j) printf("%d ", v[j]);puts("");
        ma[v] = i;
    }
}

int main() {
    n = read();
    for(int i = 1; i <= n; ++ i) a[i] = read();
    init();
    vector <int> v; v.push_back(0);
    for(int i = 1; i <= n; ++ i) v.push_back(__builtin_popcount(a[i]));
    for(int i = 0; i < (1 << lim); ++ i) {
        vector <int> v1 = v;
        for(int j = 1; j <= n; ++ j) v1[j] += b[i][j];
        vector <int> V = v1;
        for(int j = 0; j < (lim << 1); ++ j) {
            for(int k = 1; k <= n; ++ k) V[k] = j - v1[k];
            if(ma.count(V)) return printf("%d\n", i | (ma[V] << 15));
        }
    }
    puts("-1");
    return 0;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 9, B = 1e9 + 7;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 16;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 16;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 9, B = 19;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 9, B = 43;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas
typedef pair <int, int> pii;

const int mod = 1e9 + 9, B1 = 1e9 + 7, B2 = 43;
int a[100], b[100], c[100];
map <pii, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H1 = 0, H2 = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 16;
			H1 = (H1 * B1 + c[i]) % mod;
			H2 = (H2 * B2 + c[i]) % mod;
		}
		if (hash.find(pii(H1, H2)) == hash.end())
			hash[pii(H1, H2)] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H1 = 0, H2 = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 16;
			H1 = (H1 * B1 + c[i]) % mod;
			H2 = (H2 * B2 + c[i]) % mod;
		}
		if (hash.find(pii(H1, H2)) != hash.end()) {
			cout << (mask << 15) + hash[pii(H1, H2)];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 7, B = 37;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 16;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 16;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 9, B = 41;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


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

#define int long long
#define hash kalbas

const int mod = 1e9 + 9, B = 41;
int a[100], b[100], c[100];
unordered_map <int, int> hash;

int32_t main() {
	int n;
	cin >> n;
	for (int i = 0; i < n; i++) {
		cin >> a[i];
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] % (1 << 15)) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i] - b[i + 1] + 19;
			H = (H * B + c[i]) % mod;
		}
//		if (hash.find(H) == hash.end())
			hash[H] = mask;
	}
	for (int mask = 0; mask < (1 << 15); mask++) {
		for (int i = 0; i < n; i++) {
			b[i] = __builtin_popcount((a[i] >> 15) ^ mask);
		}
		int H = 0;
		for (int i = 0; i < n - 1; i++) {
			c[i] = b[i + 1] - b[i] + 19;
			H = (H * B + c[i]) % mod;
		}
		if (hash.find(H) != hash.end()) {
			cout << (mask << 15) + hash[H];
			return 0;
		}
	}
	cout << -1;
}


#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("-O3")

#include <bits/stdc++.h>

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>

#define pb push_back
#define F first
#define S second
#define lll long long
#define lld long double

#define data sdadsd

//#define int lll

using namespace std;
using namespace __gnu_pbds;

template <typename T>
using ordered_set = tree <T,null_type,less<T>,rb_tree_tag,tree_order_statistics_node_update>;

const int N = 2e5 + 229;
const lll M = 1e9 + 7;
const lll M2 = 1e9 + 9;
const lll mod = 998244353;
const int rx[4] = {0, 0, -1, 1};
const int ry[4] = {-1, 1, 0, 0};
const lld eps = 1e-11;
const double pi = acos(-1.0);

vector < vector < int > > bor;

vector < int > kek;

vector < int > what;

int a[N];

void add(vector < int > x, int mask) {
    int v = 0;
    for (auto to : x) {
        if (!bor[v][to]) {
            bor[v][to] = int(bor.size());
            bor.pb(kek);
            what.pb(0);
        }
        v = bor[v][to];
        what[v] = mask;
    }
}

int ok(vector < int > x) {
    int v = 0;
    for (auto to : x) {
        if (!bor[v][to]) {
            return -1;
        }
        v = bor[v][to];
    }
    return what[v];
}

int kol (int x) {
    int res = 0;
    while (x) {
        if (x % 2) {
            ++res;
        }
        x /= 2;
    }
    return res;
}

main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);
    srand(time(0));
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#else
//    freopen("maxxor.in", "r", stdin);
//    freopen("maxxor.out", "w", stdout);
#endif
    kek.resize(31);
    what.pb(0);
    bor.pb(kek);
    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        cin >> a[i];
    }
    vector < int > now(n);
    int k;
    int bit;
    for (int mask = 0; mask < (1 << 15); ++mask) {
        for (int i = 0; i < n; ++i) {
            k = 0;
            for (int j = 0; j < 15; ++j) {
                bit = ((1 << j) & a[i]);
                if (bit) {
                    bit = 1;
                }
                if ((1 << j) & mask) {
                    bit ^= 1;
                }
                if (bit) {
                    ++k;
                }
            }
            now[i] = k;
        }
        add(now, mask);
    }
    vector < int > need(n);
    bool f;
    int ans;
    int was = -1, hoho;
    for (int mask = 0; mask < (1 << 15); ++mask) {
        for (int i = 0; i < n; ++i) {
            k = 0;
            for (int j = 0; j < 15; ++j) {
                bit = ((1 << (j + 15)) & a[i]);
                if (bit) {
                    bit = 1;
                }
                if ((1 << j) & mask) {
                    bit ^= 1;
                }
                if (bit) {
                    ++k;
                }
            }
            now[i] = k;
        }
        for (k = 0; k <= 30; ++k) {
            f = 0;
            for (int i = 0; i < n; ++i) {
                need[i] = k - now[i];
                if (need[i] < 0) {
                    f = 1;
                    break;
                }
            }
            if (!f) {
                ans = ok(need);
                if (ans != -1) {
                    int mem = mask + ans;
                    for (int i = 0; i < n; ++i) {
                        hoho = kol(a[i] ^ mem);
                        if (was == -1) {
                            was = hoho;
                        }
                        if (hoho != was) {
                            was = -2;
                        }
                    }
                    if (was == -2) {
                        continue;
                    }
                    was = -1;
                    cout << mask + ans;
                    return 0;
                }
            }
        }

    }
    cout << -1;
    return 0;
}