N = int(input()) A = list(map(int, input().split())) A.sort() indl = 0 indr = N M = 10**5 for i in range(M): ct = 0 while indl < indr and A[indl] == i: ct += 1 indl += 1 if ct == 0: print(i) exit() if ct == 1: if indl == indr: print(i) exit() else: indr -= 1