N = int(input()) A = list(map(int,input().split())) A.sort(reverse = True) ans = 0 import sys while A: if A[-1] != ans: print(ans) exit() if len(A) <= 1: print(ans) exit() if A[-2] != ans: print(ans) exit() while A and A[-1] == ans: A.pop() ans += 1 print(ans)