#include #include #define rep(i, a, b) for (ll i = (ll)(a); i < (ll)(b); i++) using namespace atcoder; using namespace std; typedef long long ll; int main() { ios::sync_with_stdio(false); cin.tie(nullptr); cout << fixed << setprecision(12); int ans = 0; int n; cin >> n; vector a(n + 1); rep(i, 1, n + 1) { cin >> a[i]; ans |= (i & a[i]); } rep(i, 1, n + 1) { if ((ans & i) != a[i]) { cout << "-1" << endl; return 0; } } if (ans == 0) { ans = -1; rep(i, 1, 1000) { bool ok = 1; int test = 1 << i; rep(j, 1, n + 1) { if ((test & j) != a[j]) ok = 0; } if (ok) { ans = test; break; } } } cout << ans << endl; }