#include using namespace std; using ll = long long; int main() { ios::sync_with_stdio(false); cin.tie(0); int n, ans = 0; cin >> n; vector b(n); for(int i = 1; i <= n; i++){ auto &&v = b[i - 1]; cin >> v; if ((v & i) != v){ cout << "-1\n"; return 0; } ans |= (v & i); } for(int i = 1; i <= n; i++){ auto &&v = b[i - 1]; if((i & ans) != v){ cout << "-1\n"; return 0; } } if(ans != 0){ cout << ans << '\n'; return 0; } ans = 1; while(ans <= 1'000'000){ bool flg = true; for(int j = 1; j <= n; j++){ if((j & ans) != b[j - 1]){ flg = false; break; } } if(flg){ cout << ans << '\n'; return 0; } ans *= 2; } cout << "-1\n"; }