import sys input = sys.stdin.readline from collections import Counter N=int(input()) A=list(map(int,input().split())) C=Counter(sorted(A)) B=[C[i] for i in range(10**5)] ind=-1 for i in range(10**5): if B[i]==1: ind=i break for i in range(10**5): if B[i]>2 and ind!=-1: B[i]-=1 B[ind]+=1 ind=-1 for i in range(10**5-1,-1,-1): if B[i]>=1 and ind!=-1 and i>ind: B[i]-=1 B[ind]+=1 break for i in range(10**5): if B[i]<2: print(i) break