#include <bits/stdc++.h>
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math,O3")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
using namespace std;
int sz;
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> sz;
  vector<int> arr(sz);
  for (int i = 0; i < sz; i++) cin >> arr[i];
  sort(arr.begin(), arr.end());
  arr.erase(unique(arr.begin(), arr.end()), arr.end());
  for (int i = 0; i < (1 << 30); i++) {
    int c = __builtin_popcount(arr[0] ^ i);
    bool q = 0;
    for (int j = 0; j < arr.size(); j++) {
      if (__builtin_popcount(arr[j] ^ i) != c) {
        q = 1;
        break;
      }
    }
    if (!q) {
      cout << i;
      return 0;
    }
  }
  cout << "-1";
}
