This page is still under construction.

Parts of this page are still being built. What you see may change.

Two Strings

Time limit2sMemory limit512 MB

Summary
Given two digit strings s and t, find the largest difference between a number formed by a cyclic shift of s with no leading zero and a number formed by such a shift of t.
Level

Medium7 of 10

Topics
String, Greedy, String matching, Implementation
Solved
No attempts yet

Problem

The cyclic shift of a string s0s1…sn−1s_0s_1\ldots s_{n-1} by kk positions is the string sksk+1…sns1…sk−1s_ks_{k+1}\ldots s_n s_1\ldots s_{k-1}. For example, the cyclic shift of the string «abcde» by two positions is the string «cdeab». In this problem, from now on only strings consisting of the decimal digits 0 through 9 are considered. Any such string whose first character is not zero can be assigned the number whose decimal representation it is. No number is assigned to strings that begin with zero. For example, the string 123 is assigned the number one hundred twenty-three, and the string 0123 is assigned no number.

Two strings ss and tt are given. Let SS be the set of all cyclic shifts of the string ss, and TT the set of all cyclic shifts of the string tt. For example, if ss = «1234», then SS contains the strings «1234», «2341», «3412», «4123». Let NUM(A)\mathrm{NUM}(A) be the set of numbers corresponding to the strings in the set AA.

Given the strings ss and tt, write a program that finds the maximum number representable as a difference x−yx - y, where xx belongs to NUM(S)\mathrm{NUM}(S) and yy belongs to NUM(T)\mathrm{NUM}(T).

For example, if ss = «25» and tt = «12», then NUM(S)\mathrm{NUM}(S) contains the numbers 25 and 52, and NUM(T)\mathrm{NUM}(T) contains the numbers 12 and 21. Their pairwise differences are 25−12=1325 - 12 = 13, 25−21=425 - 21 = 4, 52−12=4052 - 12 = 40, 52−21=3152 - 21 = 31. The maximum of these differences is 40.

Input

The first line of the input file contains the string ss, and the second line contains the string tt. Both strings are nonempty, contain only digits, at least one of which is not zero, and have length at most 3000 characters.

Output

Output the required number without leading zeros to the output file.

Examples2

  1. Example 1

    Input
    25
    12
    
    Expected output
    40
    
  2. Example 2

    Input
    100
    1
    
    Expected output
    99