#include <bits/stdc++.h>
#pragma optimization_level 3
#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")
int bcnt[1 << 16];
int popcnt(unsigned int x) { return bcnt[x & ((1 << 16) - 1)] + bcnt[x >> 16]; }
int main() {
  for (int i = 0; i < 1 << 16; ++i) {
    int cnt = 0;
    for (int b = 0; b < 16; ++b) {
      cnt += (i >> b) & 1;
    }
    bcnt[i] = cnt;
  }
  int n;
  scanf("%d", &n);
  const int N = 100;
  int a[N];
  for (int i = 0; i < n; ++i) {
    scanf("%d", &a[i]);
  }
  std::sort(a, a + n);
  n = std::unique(a, a + n) - a;
  std::random_shuffle(a, a + n);
  int i = (1 << 30) - 1;
  while (i--) {
    int res = popcnt(a[0] ^ i);
    int j = n - 1;
    while (j > 0 && popcnt(a[j] ^ i) == res) {
      --j;
    }
    if (j == 0) {
      printf("%d", i);
      return 0;
    }
  }
  printf("-1");
  return 0;
}
