Smallest number missing from a digit sequence

Given up to 1000 digits, find the smallest non-negative integer that never occurs as a consecutive block without a leading zero.

Medium4String matchingBrute forceInterviewNo attempts yetTime limit1sMemory limit256 MB

Problem

Hanako heard that if you pick any non-negative integer, it always turns up somewhere in the decimal expansion of π=3.14159265\pi = 3.14159265\ldots. Since then, whenever she sees a sequence of digits she checks which integers appear in it.

An integer appears in the sequence if you can read it off consecutive digits, in order, as a decimal number. A value of two or more digits must not start with 0. For example, the sequence 3 0 1 contains the integers 3, 0, 1, 30 and 301, five in total.

Hanako wants the smallest non-negative integer that does not appear in the sequence. The sequence above contains 0 and 1 but not 2, so the answer is 2.

Write a program that answers Hanako's question.

Input

The first line contains the length of the sequence NN (1N10001 \le N \le 1000).

From the second line on, D1,D2,,DND_1, D_2, \ldots, D_N are given in order. Each DkD_k is an integer between 0 and 9, and neighbouring digits are separated by a space or a line break.

Output

Print the smallest non-negative integer that does not appear in the sequence.