#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; } } cout << ans << '\n'; }