#include <bits/stdc++.h>
using namespace std;
int n;
int a[112], tam[112], a1[112], a2[112];
int d[112];
map<vector<int>, int> m;
void kaisa() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int x = 0; x < (1 << 15); x++) {
    vector<int> res;
    for (int i = 0; i < n; i++) {
      res.push_back(__builtin_popcount(((a[i] ^ x) % (1 << 15))));
    }
    m[res] = x;
  }
  for (int x = 0; x < (1 << 15); x++) {
    vector<int> res;
    for (int i = 0; i < n; i++) {
      res.push_back(-__builtin_popcount((a[i] >> 15) ^ x));
    }
    for (int i = 0; i <= 30; i++) {
      if (m.count(res)) {
        int temp = (x << 15) | m[res];
        cout << temp;
        return;
      }
      for (int j = 0; j < n; j++) {
        res[j]++;
      }
    }
  }
  cout << -1;
}
int main() {
  kaisa();
  return 0;
}
