#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 & a[i] != a[i]) { cout << "-1" << endl; return 0; } } cout << ans << endl; }