#include <bits/stdc++.h>
using namespace std;
const int N = 110;
int a[N];
int cnt[1 << 15];
map<vector<int>, int> mp;
int n;
int mod;
int get(int x) {
  int res = 0;
  while (x) {
    res += x & 1;
    x >>= 1;
  }
  return res;
}
int main() {
  ios ::sync_with_stdio(false);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < 1 << 15; i++) {
    cnt[i] = get(i);
  }
  for (int i = 0; i < n; i++) cin >> a[i];
  vector<int> v(n);
  for (int i = 0; i < 1 << 15; i++) {
    for (int j = 0; j < n; j++) v[j] = cnt[(a[j] >> 15) ^ i];
    mp[v] = i;
  }
  mod = (1 << 15) - 1;
  for (int i = 0; i < n; i++) a[i] = a[i] & mod;
  vector<int> ans(n);
  for (int i = 0; i < 1 << 15; i++) {
    for (int j = 0; j < n; j++) v[j] = cnt[a[j] ^ i];
    for (int j = 0; j <= 16; j++) {
      for (int k = 0; k < n; k++) ans[k] = j - v[k];
      if (mp.count(ans)) {
        int res = i | (mp[ans] << 15);
        cout << res << endl;
        return 0;
      }
    }
  }
  cout << -1 << endl;
  return 0;
}
