#include <bits/stdc++.h>
using namespace std;
int main() {
  int n;
  cin >> n;
  int arr[n];
  for (int i = 0; i < n; i++) {
    cin >> arr[i];
  }
  map<vector<int>, int> value;
  for (int i = 0; i < (1 << 15); i++) {
    vector<int> tmp;
    for (int j = 0; j < n; j++) {
      tmp.push_back(__builtin_popcount(arr[j] / (1 << 15) ^ i));
    }
    value[tmp] = i;
  }
  for (int i = 0; i < (1 << 15); i++) {
    for (int num = 0; num <= 30; num++) {
      vector<int> tmp;
      for (int j = 0; j < n; j++) {
        tmp.push_back(num - (__builtin_popcount(arr[j] % (1 << 15) ^ i)));
      }
      if (value.count(tmp)) {
        int ans = value[tmp] * pow(2, 15) + i;
        cout << ans << endl;
        return 0;
      }
    }
  }
  cout << "-1" << endl;
  return 0;
}
