#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100 + 10;
int n, a[MAXN];
map<vector<int>, int> mp;
vector<int> dif;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  dif.clear();
  for (int i = 0; i < n; i++) cin >> a[i], dif.push_back(0);
  for (int x = 0; x < (1 << 15); x++) {
    for (int i = 0; i < n; i++) {
      dif[i] = (__builtin_popcount((a[i] >> 15) ^ x));
      if (i) dif[i] -= dif[0];
    }
    dif[0] = 0;
    mp[dif] = x;
  }
  for (int i = 0; i < n; i++) a[i] = (a[i] & ((1 << 15) - 1));
  for (int x = 0; x < (1 << 15); x++) {
    for (int i = 0; i < n; i++) {
      dif[i] = (__builtin_popcount(a[i] ^ x));
      if (i) dif[i] = dif[0] - dif[i];
    }
    dif[0] = 0;
    if (mp.count(dif)) return cout << ((mp[dif] << 15) | x) << endl, 0;
  }
  cout << -1 << endl;
  return 0;
}
