import sys from collections import defaultdict def main(): n = int(sys.stdin.readline()) a = list(map(int, sys.stdin.readline().split())) freq = defaultdict(int) for num in a: freq[num] += 1 # Compute initial mex mex = 0 while mex in freq: mex += 1 # Find the smallest x where count[x] == 1 and n > 1 candidate = None x = 0 while True: if x in freq and freq[x] == 1 and n > 1: candidate = x break if x > mex: # No need to check beyond the initial mex break x += 1 if candidate is not None: print(min(mex, candidate)) else: print(mex) if __name__ == "__main__": main()