#include <bits/stdc++.h>
using namespace std;
int N;
int arr[104];
map<vector<int>, int> mp, val;
int main() {
  scanf("%d", &N);
  for (int i = 1; i <= N; ++i) {
    scanf("%d", arr + i);
  }
  for (int i = 0; i < (1 << 15); ++i) {
    vector<int> vc;
    for (int j = 1; j <= N; ++j) {
      vc.push_back(__builtin_popcount((arr[j] & ((1 << 15) - 1)) ^ i));
    }
    mp[vc] = 1;
    val[vc] = i;
  }
  for (int i = 0; i < (1 << 15); ++i) {
    vector<int> vc;
    for (int j = 1; j <= N; ++j) {
      vc.push_back(
          __builtin_popcount((arr[j] & (((1 << 15) - 1) << 15)) ^ (i << 15)));
    }
    for (int j = 0; j <= 30; ++j) {
      vector<int> fd;
      int f = 1;
      for (int x = 0; x < N; ++x) {
        if (j - vc[x] < 0) {
          f = 0;
          break;
        }
        fd.push_back(j - vc[x]);
      }
      if (f && mp[fd]) {
        printf("%d\n", (i << 15) + val[fd]);
        return 0;
      }
    }
  }
  puts("-1");
  return 0;
}
