#include <bits/stdc++.h>
using namespace std;
int n;
bool flag;
int num[33000];
vector<int> G;
map<vector<int>, int> vis;
int popcount(int x) {
  int res = 0;
  while (x) {
    x -= x & -x;
    ++res;
  }
  return res;
}
int main() {
  scanf("%d", &n);
  vis.clear();
  for (int i = 1; i <= n; ++i) scanf("%d", &num[i]);
  for (int i = 0; i < (1 << 15); ++i) {
    G.clear();
    int tmp = popcount(i ^ (num[1] & ((1 << 15) - 1)));
    for (int j = 2; j <= n; ++j) {
      int x = i ^ (num[j] & ((1 << 15) - 1));
      G.push_back(popcount(x) - tmp);
    }
    vis[G] = i;
  }
  for (int i = 0; i < (1 << 15); ++i) {
    G.clear();
    int tmp = popcount(i ^ (num[1] >> 15));
    for (int j = 2; j <= n; ++j) {
      int x = i ^ (num[j] >> 15);
      G.push_back(tmp - popcount(x));
    }
    if (vis.count(G)) {
      printf("%d\n", (i << 15) + vis[G]);
      flag = 1;
      break;
    }
  }
  if (flag == 0) puts("-1");
  return 0;
}
