#include <bits/stdc++.h>
using namespace std;
const int inf = 0x3f3f3f3f;
const int maxn = 1e5 + 10;
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int n, m, q, k;
  long long a[maxn], b[maxn];
  cin >> n;
  for (int i = 1; i <= n; i++) cin >> a[i];
  map<vector<int>, int> mp;
  int f = 0;
  int ans1 = 0, ans2 = 0;
  vector<int> v;
  for (int i = 0; i < (1 << 15); i++) {
    v.clear();
    for (int j = 1; j <= n; j++) {
      v.push_back(__builtin_popcount(i ^ (a[j] & ((1 << 15) - 1))));
    }
    for (int j = 0; j < n - 1; j++) {
      v[j] = v[j + 1] - v[j];
    }
    v.pop_back();
    if (mp[v] == 0) mp[v] = i;
  }
  for (int i = 0; i < (1 << 15); i++) {
    v.clear();
    for (int j = 1; j <= n; j++) {
      v.push_back(__builtin_popcount(i ^ (a[j] >> 15)));
    }
    for (int j = 0; j < n - 1; j++) {
      v[j] = -v[j + 1] + v[j];
    }
    v.pop_back();
    if (mp[v]) {
      cout << mp[v] + (i << 15);
      return 0;
    }
  }
  cout << -1;
}
