#include <bits/stdc++.h>
using namespace std;
using VI = vector<char>;
VI s;
map<VI, int> mp;
int b[32768], x[100], y[100];
int main() {
  for (int i = 32768; i--;) b[i] = __builtin_popcount(i);
  int n;
  scanf("%d", &n);
  for (int i = n; i--;) {
    int u;
    scanf("%d", &u);
    x[i] = u & (32768 - 1), y[i] = u >> 15;
  }
  for (int i = 32768; i--;) {
    s.clear();
    for (int j = 1, st = b[i ^ y[0]]; j < n; j++) {
      s.push_back(b[i ^ y[j]] - st);
    }
    if (mp.find(s) == mp.end()) mp[s] = i;
  }
  for (int i = 32768; i--;) {
    s.clear();
    for (int j = 1, st = b[i ^ x[0]]; j < n; j++) {
      s.push_back(st - b[i ^ x[j]]);
    }
    if (mp.find(s) != mp.end()) printf("%d\n", i + (mp[s] << 15)), exit(0);
  }
  puts("-1");
}
