eolymp
bolt
Try our new interface for solving problems
Problems

Small Number

Small Number

Given positive integer k and the set of digits s. Find the minimum integer, bigger or equal to k, which decimal notation contains the digits from set s only. The decimal notation of a number must not contain two consecutive identical digits.

Input

First line contains the set of valid digits s in increasing order without spaces and repetitions. Second line contains number k (1k105).

Output

Print one required number. If the number does not exist, print -1.

Time limit 1 second
Memory limit 128 MiB
Input example #1
12
7
Output example #1
12
Input example #2
0
5
Output example #2
-1
Source 2018 Azerbaijan School Competition, II Stage, April 8, Problem B