#include <bits/stdc++.h>
using namespace std;
int n, a[200];
map<vector<int>, int> dp;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n;
  vector<int> p(n, 0);
  for (int i = 0; i < n; i++) {
    cin >> a[i];
  }
  for (int mask = 0; mask < (1 << 15); mask++) {
    int minx = 30;
    for (int i = 0; i < n; i++) {
      p[i] = __builtin_popcount((a[i] & ((1 << 15) - 1)) ^ mask);
      minx = min(minx, p[i]);
    }
    for (int i = 0; i < n; i++) {
      p[i] -= minx;
    }
    dp[p] = mask;
  }
  for (int mask = 0; mask < (1 << 15); mask++) {
    int maxn = 0;
    for (int i = 0; i < n; i++) {
      p[i] = __builtin_popcount((a[i] >> 15) ^ mask);
      maxn = max(maxn, p[i]);
    }
    for (int i = 0; i < n; i++) {
      p[i] = maxn - p[i];
    }
    if (dp.find(p) != dp.end()) {
      cout << (dp[p] ^ (mask << 15));
      return 0;
    }
  }
  cout << -1;
  return 0;
}
