#include <bits/stdc++.h>
using namespace std;
unordered_map<int, int> M;
unordered_map<int, int> M2;
const int MOD = 1e9 + 7;
const int MOD2 = 1e9 + 9;
inline int f(vector<int>& v) {
  int n = (int)v.size();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = ((long long)ans * 30 + v[i] + 30) % MOD;
  }
  return ans;
}
inline int f2(vector<int>& v) {
  int n = (int)v.size();
  int ans = 0;
  for (int i = 0; i < n; i++) {
    ans = ((long long)ans * 30 + v[i] + 30) % MOD2;
  }
  return ans;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> v(n);
  for (int i = 0; i < n; i++) cin >> v[i];
  for (int mask = 0; mask < (1 << 15); mask++) {
    vector<int> cur;
    for (int j = 0; j < n; j++) {
      int sum = 0;
      for (int i = 0; i < 15; i++) {
        if (mask & (1 << i)) {
          if (((v[j] & (1 << (i + 15))) == 0)) {
            sum++;
          }
        } else {
          if (v[j] & (1 << (i + 15))) {
            sum++;
          }
        }
      }
      cur.push_back(sum);
    }
    M[f(cur)] = mask;
    M2[f2(cur)] = mask;
  }
  int ans = -1;
  for (int mask = 0; mask < (1 << 15); mask++) {
    vector<int> cur;
    for (int j = 0; j < n; j++) {
      int sum = 0;
      for (int i = 0; i < 15; i++) {
        if (mask & (1 << i)) {
          if (((v[j] & (1 << i)) == 0)) {
            sum++;
          }
        } else {
          if (v[j] & (1 << i)) {
            sum++;
          }
        }
      }
      cur.push_back(sum);
    }
    for (int tam = 0; tam <= 30; tam++) {
      vector<int> cur2(n);
      for (int j = 0; j < n; j++) {
        cur2[j] = tam - cur[j];
      }
      int val = f(cur2);
      if (ans == -1 && M.count(val) && M2.count(f2(cur2))) {
        ans = (M[val] << 15) + mask;
      }
    }
  }
  cout << ans << endl;
}
