#include <bits/stdc++.h>
using namespace std;
int n;
long long a[105];
long long b[105];
long long k;
map<string, long long> mp;
long long solve() {
  long long k;
  string rez;
  for (int i = 0; i < (1 << 15); i++) {
    rez = "";
    for (int j = 0; j < n; j++) {
      k = a[j] ^ i;
      int count = 0;
      for (int t = 0; t < 15; t++) {
        count += k % 2;
        k = k >> 1;
      }
      rez += ('a' + count);
    }
    char ch_min = rez[0];
    for (int j = 0; j < n; j++) {
      ch_min = min(ch_min, rez[j]);
    }
    for (int j = 0; j < n; j++) {
      rez[j] -= (ch_min - 'a');
    }
    mp[rez] = i;
  }
  return 0;
}
long long calc() {
  long long k;
  string rez;
  for (int i = 0; i < (1 << 15); i++) {
    rez = "";
    for (int j = 0; j < n; j++) {
      k = b[j] ^ i;
      int count = 0;
      for (int t = 0; t < 15; t++) {
        count += k % 2;
        k = k >> 1;
      }
      rez += ('a' + count);
    }
    char ch_min = rez[0];
    for (int j = 0; j < n; j++) {
      ch_min = min(ch_min, rez[j]);
    }
    for (int j = 0; j < n; j++) {
      rez[j] -= (ch_min - 'a');
    }
    char ch_max = rez[0];
    for (int j = 0; j < n; j++) {
      ch_max = max(ch_max, rez[j]);
    }
    for (int j = 0; j < n; j++) {
      rez[j] = 'a' + (int(ch_max) - int(rez[j]));
    }
    if (mp.count(rez)) {
      long long ans = mp[rez];
      ans = ans << 15;
      ans += i;
      return ans;
    }
  }
  return -1;
}
int main() {
  cin >> n;
  long long p = (1 << 15);
  for (int i = 0; i < n; i++) {
    cin >> k;
    a[i] = k / p;
    b[i] = k % p;
  }
  solve();
  cout << calc();
  return 0;
}
