#include <bits/stdc++.h>
using namespace std;
template <class T>
using minheap = priority_queue<T, vector<T>, greater<T>>;
const int N = 110;
const int B = 30;
const int H = 15;
int n;
int a[N];
int pc(int x) { return __builtin_popcount(x); }
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  sort(a, a + n);
  map<vector<int>, int> left;
  for (int m = 0; m < (1 << H); m++) {
    vector<int> ones(n);
    for (int i = 0; i < n; i++) {
      ones[i] = pc((a[i] >> H) ^ m);
    }
    left[ones] = m;
  }
  for (int m = 0; m < (1 << H); m++) {
    vector<int> ones(n);
    for (int i = 0; i < n; i++) {
      ones[i] = pc((a[i] & ((1 << H) - 1)) ^ m);
    }
    int mx = *max_element(ones.begin(), ones.end());
    for (int target = mx; target <= B; target++) {
      vector<int> delta(n);
      for (int i = 0; i < n; i++) {
        delta[i] = target - ones[i];
      }
      auto it = left.find(delta);
      if (it != left.end()) {
        int L = it->second;
        int R = m;
        cout << (L << H) + R << '\n';
        return 0;
      }
    }
  }
  cout << -1 << '\n';
  return 0;
}
