#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200, N = 60000 + 20, mod = 1e9 + 7, INF = 1e9, lg = 32;
int a[maxn], b[maxn], c[maxn];
vector<int> cprime;
map<vector<int>, int> mp;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> a[i];
    cprime.push_back(0);
  }
  for (int i = 0; i < (1ll << 15); ++i) {
    for (int j = 0; j < n; ++j) {
      b[j] = (a[j] >> 15);
    }
    for (int j = 0; j < n; ++j) {
      b[j] ^= i;
      c[j] = __builtin_popcount(b[j]);
      cprime[j] = (c[j] - c[0]);
    }
    mp[cprime] = i;
  }
  for (int i = 0; i < (1ll << 15); ++i) {
    for (int j = 0; j < n; ++j) {
      b[j] = (a[j] & ((1ll << 15) - 1));
    }
    for (int j = 0; j < n; ++j) {
      b[j] ^= i;
      c[j] = __builtin_popcount(b[j]);
      cprime[j] = (c[0] - c[j]);
    }
    if (mp.count(cprime)) {
      long long ans = (mp[cprime] << 15) ^ i;
      cout << ans << '\n';
      return 0;
    }
  }
  cout << -1 << '\n';
}
