Submit solution

Points: 10 (partial)
Time limit: 0.5s
Memory limit: 555M

Author:
Problem types

Natalie has mastered counting from 1 to 10 on her fingers. With 5 fingers in each of 2 hands, this may seem like the limit, but with her immense intelligence, she realized she could count even further using concatenation!

For example, Natalie can hold up 1 finger on her left hand and 3 fingers on her right hand to represent the number 13. By ensuring that she separates her hands far enough, this representation will not be confused with 4. With this technique, Natalie can count starting from 1 all the way up to 15, since 16 is the smallest number that cannot be expressed in this way.

Natalie has also begun making friends in primary school, and she has told everybody of her counting technique, making her the coolest kid in the class. With the help of her followers and a lot of teamwork, they can count together with a total of N hands, ordered from left to right. Each hand can hold anywhere from 0 to 5 fingers up.

To generalize Natalie's counting technique, she can choose to partition the hands into some number of contiguous groups. Adjacent groups will be separated sufficiently to indicate concatenation. The final represented number will be the concatenation of the total number of fingers in each contiguous group, from left to right. Addition is always done before concatenation, and leading zeros are permitted.

For example, 125 could be represented on 4 hands as [3+4+5][5], and 708 could be represented on 5 hands as [5+2][0][4+4].

Natalie and her friends are trying to count starting from X. Can you help them determine the smallest number greater than or equal to X that cannot be represented on N hands?

Constraints

1 \le N \le 10^6

Subtask 1 [30%]

X = 1

Subtask 2 [70%]

1 \le X < 10^{10^6}

Input Specification

The first line contains one integer, N, the number of hands.

The next line contains one integer, X, the number that Natalie and her friends will try to start counting from.

Output Specification

Output one line containing one integer, the smallest number greater than or equal to X that cannot be represented using N hands.

Sample Input 1

2
1

Sample Output 1

16

Explanation for Sample 1

This is the example described in the problem statement.

Sample Input 2

5
1

Sample Output 2

666

Explanation for Sample 2

It can be shown that all numbers from 1 to 665 can be represented on 5 hands.

For example, 23 can be represented on 5 hands as [5+5+5+5+3], 76 can be represented as [4+1+2][1+5], 579 can be represented as [5][3+4][5+4], and 665 can be represented as [5+1][5+1][5].

It can be shown that 666 cannot be represented on 5 hands. Thus, it is the smallest number greater than or equal to 1 which cannot be represented on 5 hands.

Sample Input 3

5
148293

Sample Output 3

148293

Explanation for Sample 3

In this sample, Natalie and her friends are trying to count starting from X=148293. However, 148293 already cannot be represented on 5 hands! Thus, 148293 is the smallest number greater than or equal to X which cannot be represented on 5 hands.

Sample Input 4

15
31415926535

Sample Output 4

31415926666

Comments

There are no comments at the moment.