#include <bits/stdc++.h>
using namespace std;
const int maxn = 100 + 7;
const int maxm = (1 << 15) + 7;
struct Node {
  int ans;
  vector<int> ve;
  bool operator<(const Node &a) const { return ve < a.ve; }
} node;
set<Node> se;
int l[maxn], h[maxn];
int getlen[maxm], n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int x;
    scanf("%d", &x);
    l[i] = (x & ((1 << 15) - 1));
    h[i] = x >> 15;
  }
  for (int i = 0; i < maxm; i++) {
    int v = i;
    while (v) {
      if (v & 1) getlen[i]++;
      v /= 2;
    }
  }
  int len = (1 << 15);
  for (int i = 0; i < len; i++) {
    node.ans = i;
    node.ve.clear();
    int v = getlen[i ^ l[1]];
    for (int j = 2; j <= n; j++) node.ve.push_back(v - getlen[i ^ l[j]]);
    se.insert(node);
  }
  for (int i = 0; i < len; i++) {
    node.ans = i;
    node.ve.clear();
    int v = getlen[i ^ h[1]];
    for (int j = 2; j <= n; j++) node.ve.push_back(getlen[i ^ h[j]] - v);
    if (se.find(node) != se.end()) {
      Node now = *se.find(node);
      printf("%d\n", (i << 15) + now.ans);
      return 0;
    }
  }
  printf("-1\n");
  return 0;
}
