#include <bits/stdc++.h>
using namespace std;
const int N = 100 + 20;
const int LG = 15;
int a[N], cnt[N];
map<vector<int>, int> mp;
int main() {
  ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
    cnt[i] = __builtin_popcount(a[i]);
  }
  for (int mask = 0; mask < (1 << LG); mask++) {
    vector<int> vec(n + 20, 0);
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < LG; j++) {
        if (!((mask >> j) & 1)) continue;
        vec[i] += (((a[i] >> j) & 1) ? -1 : 1);
      }
      vec[i] += cnt[i];
    }
    mp[vec] = mask;
  }
  for (int mask = 0; mask < (1 << 30); mask += (1 << LG)) {
    vector<int> vec(n + 20, 0);
    for (int i = 0; i < n; i++) {
      for (int j = LG; j < 30; j++) {
        if (!((mask >> j) & 1)) continue;
        vec[i] += (((a[i] >> j) & 1) ? -1 : 1);
      }
      vec[i] *= -1;
    }
    for (int i = 0; i <= 30; i++) {
      auto it = mp.find(vec);
      if (it != mp.end()) return cout << (mask | it->second) << endl, 0;
      for (int j = 0; j < n; j++) vec[j]++;
    }
  }
  cout << -1 << endl;
  return 0;
}
