n,*b=map(int,open(0).read().split()) x=0 for i in range(1,n+1): a=b[i-1]&i x|=a for i in range(1,n+1): if i&x!=b[i-1]: exit(print(-1)) print(x)