#include <bits/stdc++.h>
using namespace std;
int a[101], b[101];
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) cin >> a[i];
  map<vector<int>, int> mp;
  for (int mask = 0; mask < (1 << 15); mask++) {
    for (int i = 0; i < n; i++) {
      b[i] = a[i] ^ mask;
      int s = 0;
      for (int c = 0; c < 15; c++) s += ((b[i] & (1 << c)) > 0);
      b[i] = s;
    }
    vector<int> s;
    for (int i = 0; i < n; i++) s.push_back(b[0] - b[i]);
    mp[s] = mask;
  }
  for (int mask = 0; mask < (1 << 15); mask++) {
    for (int i = 0; i < n; i++) {
      b[i] = a[i] ^ (mask << 15);
      int s = 0;
      for (int c = 15; c <= 30; c++) s += ((b[i] & (1 << c)) > 0);
      b[i] = s;
    }
    vector<int> s;
    for (int i = 0; i < n; i++) s.push_back(b[i] - b[0]);
    if (mp.count(s)) {
      cout << mp[s] + (mask << 15);
      return 0;
    }
  }
  cout << -1;
}
