n = int(input()) a = list(map(int,input().split())) # 0, 0, 1, 2, 2, 3, 3 # -> 0, 0, 1, 1, 2, 2, 3 # の感じで1回だけ2にして安定させられる # あとはまねっこ mex = [0] * 100002 for i in range(n): mex[a[i]] += 1 mode = 1 g = 0 for i in range(100002): if mex[i] == 1: if mode == 1: for j in range(i): if mex[j] >= 3: mex[j] -= 1 mex[i] += 1 mode = 0 if mode: if mex[j] <= 1: g = 1 for j in range(i+1, 100002): if mex[j] >= 3 or (g == 1 and mex[j] >= 1): mex[j] -= 1 mex[i] += 1 mode = 0 if mode: if max(a) == i: print(i) exit() else: mex[max(a)] -= 1 mex[i] += 1 mode = 0 else: print(i) exit() if mex[i] == 0: print(i) exit()