#include <bits/stdc++.h>
using namespace std;
int n;
int a[105];
int cnt[2][1 << 15][105];
map<vector<int>, int> comp;
int sum_of_digits(int a) {
  if (a == 0)
    return 0;
  else if (a % 2)
    return sum_of_digits(a >> 1) + 1;
  else
    return sum_of_digits(a >> 1);
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%d", &a[i]);
  }
  if (n == 1) {
    printf("%d", 0);
    return 0;
  }
  for (int i = 0; i < (1 << 15); i++) {
    for (int j = 1; j <= n; j++) {
      cnt[0][i][j] = sum_of_digits((a[j] % (1 << 15)) ^ i);
    }
    vector<int> tym;
    for (int j = 2; j <= n; j++) {
      tym.push_back(cnt[0][i][1] - cnt[0][i][j]);
    }
    comp.insert({tym, i});
  }
  for (int i = 0; i < (1 << 15); i++) {
    for (int j = 1; j <= n; j++) {
      cnt[1][i][j] = sum_of_digits((a[j] / (1 << 15)) ^ i);
    }
    vector<int> tym;
    for (int j = 2; j <= n; j++) {
      tym.push_back(cnt[1][i][j] - cnt[1][i][1]);
    }
    if (comp.find(tym) != comp.end()) {
      int ans = comp.find(tym)->second;
      ans += i * (1 << 15);
      printf("%d", ans);
      return 0;
    }
  }
  printf("%d", -1);
  return 0;
}
