/* -*- coding: utf-8 -*- * * 3249.cc: No.3249 AND - yukicoder */ #include #include using namespace std; /* constant */ const int MAX_N = 1000000; /* typedef */ /* global variables */ int bs[MAX_N + 1]; /* subroutines */ /* main */ int main() { int n; scanf("%d", &n); for (int i = 1; i <= n; i++) scanf("%d", bs + i); int x = 0; for (int i = 1; i <= n; i++) x |= bs[i]; for (int i = 1; i <= n; i++) { if ((bs[i] & i) != bs[i]) { puts("-1"); return 0; } x &= (~i | bs[i]); } bool f = true; for (int i = 1; f && i <= n; i++) f = (bs[i] == (i & x)); printf("%d\n", f ? x : -1); return 0; }