#include <bits/stdc++.h>
using namespace std;
const int NN = (1 << 15);
int pre[NN], suf[NN];
map<string, int> mp;
int main() {
  int n;
  cin >> n;
  for (int i = 1, x; i <= n; i++) {
    cin >> x;
    pre[i] = x >> 15;
    suf[i] = x & (NN - 1);
  }
  for (int i = 0; i < NN; i++) {
    string tmp;
    for (int j = 2; j <= n; j++) {
      tmp.push_back(__builtin_popcount(i ^ pre[j]) -
                    __builtin_popcount(i ^ pre[1]));
    }
    if (!mp.count(tmp)) mp[tmp] = i;
  }
  for (int i = 0; i < NN; i++) {
    string tmp;
    for (int j = 2; j <= n; j++) {
      tmp.push_back(__builtin_popcount(i ^ suf[1]) -
                    __builtin_popcount(i ^ suf[j]));
    }
    if (mp.count(tmp)) {
      cout << (mp[tmp] << 15) + i << endl;
      return 0;
    }
  }
  puts("-1");
  return 0;
}
