#include #define fp(i, a, b) for (int i(a), ed(b); i <= ed; ++i) #define fb(i, a, b) for (int i(a), ed(b); i >= ed; --i) #define go(u, i) for (int i(head[u]); i; i = e[i].nxt) using namespace std; typedef long long Int; typedef pair pii; //////////////////////////////////////////////////////////////////////////////////////////////////// template inline void chkmin(T1& a, T2 b) { if (a > b) a = b; } template inline void chkmax(T1& a, T2 b) { if (a < b) a = b; } namespace FastIO { char buf[1 << 21], *p1 = buf, *p2 = buf; #define getchar() (p1 == p2 && (p1 = buf, p2 = (p1 + fread(buf, 1, 1 << 21, stdin))) == p1 ? EOF : *p1++) template inline T read() { T x = 0, w = 0; char ch = getchar(); while (ch < '0' || ch > '9') w |= (ch == '-'), ch = getchar(); while ('0' <= ch && ch <= '9') x = x * 10 + (ch ^ '0'), ch = getchar(); return w ? -x : x; } template inline void write(T x) { if (!x) return; write(x / 10), putchar((x % 10) ^ '0'); } template inline void print(T x) { if (x > 0) write(x); else if (x < 0) putchar('-'), write(-x); else putchar('0'); } template inline void print(T x, char en) { print(x), putchar(en); } }; // namespace FastIO using namespace FastIO; #undef getchar() const int maxn = 2e5 + 10, maxm = 20, mod = 998244353; int n, b[maxn]; bool h1[maxm], h2[maxm]; int main() { n = FastIO::read(); fp(i, 1, n) b[i] = FastIO::read(); for (int i = 1; i <= n; i++) { for (int t = 0; t < maxm; t++) { if (b[i] >> t & 1) { h1[t] = 1; } else if ((i >> t & 1) && !(b[i] >> t & 1)) h2[t] = 1; } } int x = 0; for (int t = 0; t < maxm; t++) { if (h1[t]) x |= 1 << t; } if (x == 0) { FastIO::print(-1, '\n'); return 0; } for (int i = 1; i <= n; i++) { if ((i & x) != b[i]) { FastIO::print(-1, '\n'); return 0; } } FastIO::print(x, '\n'); return 0; }