Closest Permutation Numbers

Time limit1sMemory limit128 MB

Problem

You are given two positive integers a and b with the same length. Neither number starts with 0.

You may rearrange the digits of b, using each digit exactly once, to make another positive integer of the same length. The resulting number also must not start with 0.

Find the following two values among all valid rearrangements.

  • The smallest number that is greater than or equal to a
  • The largest number that is less than a

If one of these values does not exist, output 0 in its place.

Input

The input has two lines.

The first line contains a, and the second line contains b. Both numbers have the same length n, where 1 <= n <= 60.

Output

On the first line, output the smallest number that can be made with all digits of b and is greater than or equal to a.

On the second line, output the largest number that can be made with all digits of b and is less than a.

If the required number for a line does not exist, output 0 on that line.