#include <bits/stdc++.h>
using namespace std;
string b[1 << 15];
int x[100], y[100], n;
map<string, int> mp;
int main() {
  scanf("%d", &n);
  for (int i = 0, z; i < n; i++) {
    scanf("%d", &z);
    x[i] = z & ((1 << 15) - 1);
    y[i] = z >> 15;
  }
  for (int B = 0; B < (1 << 15); B++) {
    for (int i = 1, j = __builtin_popcount(B ^ y[0]); i < n; i++) {
      b[B].push_back(__builtin_popcount(B ^ y[i]) - j);
    }
    if (!mp.count(b[B])) mp[b[B]] = B;
  }
  for (int B = 0; B < 1 << 15; B++) {
    string tmp;
    for (int i = 1, j = __builtin_popcount(B ^ x[0]); i < n; i++) {
      tmp.push_back(j - __builtin_popcount(B ^ x[i]));
    }
    if (mp.count(tmp)) {
      printf("%d\n", B + (mp[tmp] << 15));
      return 0;
    }
  }
  puts("-1");
  return 0;
}
