#include <bits/stdc++.h>
using namespace std;
int a[1010];
map<vector<int>, int> mp;
int main() {
  int n;
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0; i < (1 << 15); i++) {
    int cnt = __builtin_popcount((a[1] & ((1 << 15) - 1)) ^ i);
    vector<int> adj;
    for (int j = 2; j <= n; j++) {
      int temp = __builtin_popcount((a[j] & ((1 << 15) - 1)) ^ i);
      adj.push_back(cnt - temp);
    }
    if (!mp[adj]) mp[adj] = i + 1;
  }
  for (int i = 0; i < (1 << 15); i++) {
    int cnt = __builtin_popcount((a[1] >> 15) ^ i);
    ;
    vector<int> adj;
    for (int j = 2; j <= n; j++) {
      int temp = __builtin_popcount((a[j] >> 15) ^ i);
      adj.push_back(temp - cnt);
    }
    if (mp[adj]) {
      int ans = (i << 15) + mp[adj] - 1;
      printf("%d\n", ans);
      return 0;
    }
  }
  puts("-1");
  return 0;
}
