#include <bits/stdc++.h>
using namespace std;
#pragma GCC diagnostic ignored "-Wmissing-declarations"
inline int safe_mul(const int x, const int y)
    __attribute__((warn_unused_result));
int const maxn = (1 << 15) * 105;
map<int, int> ne[maxn];
int vals[maxn];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0), cout.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (int& x : a) cin >> x;
  unsigned int hmask = (1 << 15) - 1;
  int sz = 1;
  for (unsigned int mask = 0; mask <= hmask; ++mask) {
    auto p = __builtin_popcount((a[0] ^ mask) & hmask);
    int cur = 0;
    for (int i = 1; i < n; ++i) {
      auto t = __builtin_popcount((a[i] ^ mask) & hmask);
      if (!ne[cur].count(t - p)) {
        ne[cur][t - p] = sz++;
        assert(sz < maxn);
      }
      cur = ne[cur][t - p];
      p = t;
    }
    vals[cur] = mask;
  }
  unsigned int amask = (1 << 30) - 1 - hmask;
  for (unsigned int mask = 0; mask <= hmask; ++mask) {
    auto cmask = mask << 15;
    int cur = 0;
    auto p = __builtin_popcount((a[0] ^ cmask) & amask);
    for (int i = 1; i < n; ++i) {
      auto t = __builtin_popcount((a[i] ^ cmask) & amask);
      if (!ne[cur].count(p - t)) {
        cur = -1;
        break;
      }
      cur = ne[cur][p - t];
      p = t;
    }
    if (cur >= 0) {
      {
        cout << (cmask + vals[cur]) << '\n';
        exit(0);
      };
    }
  }
  {
    cout << (-1) << '\n';
    exit(0);
  };
}
