N=int(input()) B=list(map(int,input().split())) x=0 for i,b in enumerate(B,1): if b>i:exit(print(-1)) x|=b print(x)