#include <bits/stdc++.h>
inline long long ceil_div(long long a, long long b) { return (a + b - 1) / b; }
using namespace std;
inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-') fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + (c & 15);
    c = getchar();
  }
  return f * fu;
}
const int N = 105, MOD = 1e9 + 3, base = 9983253;
int n, a[N];
map<vector<int>, int> b;
int main() {
  scanf("%d", &n);
  vector<int> x(n - 1);
  for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
  for (int i = 0, u; i < 32768; i++) {
    u = __builtin_popcount((a[1] & 32767) ^ i);
    for (int j = 2; j <= n; j++)
      x[j - 2] = __builtin_popcount((a[j] & 32767) ^ i) - u;
    b[x] = i;
  }
  for (int i = 0, u; i < 32768; i++) {
    u = __builtin_popcount((a[1] >> 15) ^ i);
    for (int j = 2; j <= n; j++)
      x[j - 2] = u - __builtin_popcount((a[j] >> 15) ^ i);
    if (b.find(x) != b.end()) return printf("%d\n", (i << 15) | b[x]), 0;
  }
  return puts("-1"), 0;
}
