#include <bits/stdc++.h>
using namespace std;
const int MX = 32767;
map<vector<int>, int> d;
int N, A[105];
template <class T>
inline void read(register T &m) {
  m = 0;
  register char c = getchar();
  while (!isdigit(c)) c = getchar();
  while (isdigit(c)) m = (m << 3) + (m << 1) + (c & 15), c = getchar();
}
int main() {
  read(N);
  for (register int i = 0; i < N; ++i) {
    read(A[i]);
  }
  for (int i = 0, t; i <= MX; ++i) {
    register vector<int> x(N - 1);
    t = __builtin_popcount(i ^ (A[0] & MX));
    for (register int j = 1; j < N; ++j) {
      x[j - 1] = __builtin_popcount(i ^ (A[j] & MX)) - t;
    }
    d[x] = i;
  }
  for (register int i = 0, t; i <= MX; ++i) {
    register vector<int> x(N - 1);
    t = __builtin_popcount(i ^ (A[0] >> 15));
    for (register int j = 1; j < N; ++j) {
      x[j - 1] = t - __builtin_popcount(i ^ (A[j] >> 15));
    }
    auto itrt = d.find(x);
    if (itrt != d.end()) {
      printf("%d\n", (MX + 1) * i + itrt->second);
      return 0;
    }
  }
  puts("-1");
  return 0;
}
