#include <bits/stdc++.h>
using namespace std;
const int N2 = 15;
int32_t main() {
  ios::sync_with_stdio(false);
  cout.tie(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> vec(n);
  for (int i = 0; i < n; ++i) {
    cin >> vec[i];
  }
  map<vector<int>, int> d1;
  for (int i = 0; i < (1 << N2); ++i) {
    vector<int> cur(n);
    for (int j = 0; j < n; ++j) {
      cur[j] = __builtin_popcount((vec[j] & ((1 << N2) - 1)) ^ i);
    }
    vector<int> cc(n - 1);
    for (int j = 0; j + 1 < n; ++j) {
      cc[j] = cur[j + 1] - cur[j];
    }
    d1[cc] = i;
  }
  for (int i = (1 << N2) - 1; i >= 0; --i) {
    vector<int> cur(n);
    for (int j = 0; j < n; ++j) {
      cur[j] = __builtin_popcount((vec[j] >> N2) ^ i);
    }
    vector<int> cc(n - 1);
    for (int j = 0; j + 1 < n; ++j) {
      cc[j] = cur[j] - cur[j + 1];
    }
    if (d1.count(cc)) {
      cout << d1[cc] + (i << N2) << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
