#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  vector<int> a(n);
  for (auto &i : a) cin >> i;
  for (int x = 0; x != (1 << 30); ++x) {
    int count = __builtin_popcount(a.back() ^ x);
    bool flag = 1;
    for (auto &i : a) {
      if (__builtin_popcount(i ^ x) != count) {
        flag = 0;
        break;
      }
    }
    if (flag) {
      cout << x << endl;
      return 0;
    }
  }
  cout << -1 << endl;
  return 0;
}
