Vasya decided to pass a very large integer n to Kate. First, he wrote that number as a string, then he appended to the right integer k — the number of digits in n. 

Magically, all the numbers were shuffled in arbitrary order while this note was passed to Kate. The only thing that Vasya remembers, is a non-empty substring of n (a substring of n is a sequence of consecutive digits of the number n).

Vasya knows that there may be more than one way to restore the number n. Your task is to find the smallest possible initial integer n. Note that decimal representation of number n contained no leading zeroes, except the case the integer n was equal to zero itself (in this case a single digit 0 was used).


-----Input-----

The first line of the input contains the string received by Kate. The number of digits in this string does not exceed 1 000 000.

The second line contains the substring of n which Vasya remembers. This string can contain leading zeroes. 

It is guaranteed that the input data is correct, and the answer always exists.


-----Output-----

Print the smalles integer n which Vasya could pass to Kate.


-----Examples-----
Input
003512
021

Output
30021

Input
199966633300
63

Output
3036366999

Difficulty: interview

Starter code:

Solutions:
import math
from collections import Counter

s = list(map(int, input()))
substr = input().rstrip()
t = list(map(int, substr))

m = len(s)
x, y = 0, m
z = (x + y) // 2
while z != x:
    if z + math.floor(math.log10(z)) + 1 <= m:
        x = z
    else:
        y = z
    z = (x + y)//2
m1 = z
k = math.floor(math.log10(m1)) + 1

D = Counter(s)
D.subtract(list(map(int, str(m1))))
D.subtract(t)
try:
    c1 = min(i for i in range(1, 10) if D[i] > 0)
    c2 = t[0]

    D[c1] -= 1
    _prefix = [c1]
    
    for c in range(c2):
        _prefix += [c] * D[c]
    _suffix = []
    for c in range(c2 + 1, 10):
        _suffix += [c] * D[c]
    num = ''.join([str(c2)] * D[c2])
    prefix = ''.join(map(str, _prefix))
    suffix = ''.join(map(str, _suffix))

    if c2 == 0:
        print((min(prefix + substr + num + suffix,
                  prefix + num + substr + suffix)))
    else:
        D[c1] += 1
        st = []
        for c in range(10):
            st += [c] * D[c]
        print((min(prefix + substr + num + suffix,
                  prefix + num + substr + suffix,
                  substr + ''.join(map(str, st)))))
except ValueError:
    print(substr + '0'*D[0])


def main():
    s = input()
    if s in ("01", "10"):
        print(0)
        return
    cnt = [0] * 58
    for j in map(ord, s):
        cnt[j] += 1
    n, s1 = sum(cnt), input()
    for le in range(n - 1, 0, -1):
        if len(str(le)) + le == n:
            break
    for s in s1, str(le):
        for j in map(ord, s):
            cnt[j] -= 1
    res = [''.join([s1] + [chr(i) * a for i, a in enumerate(cnt) if a])] if s1[0] > '0' else []
    for i in range(49, 58):
        if cnt[i]:
            cnt[i] -= 1
            l = [chr(i) * a for i, a in enumerate(cnt) if a]
            l.append(s1)
            res.append(''.join([chr(i)] + sorted(l)))
            break
    print(min(res))


def __starting_point():
    main()

__starting_point()

import sys

def main():
    a = sys.stdin.readline().strip()
    b = sys.stdin.readline().strip()

    if a == "01" or a == "10":
        print("0")
        return

    cnt = [0] * 256
    for i in map(ord, a):
        cnt[i] += 1
    n = sum(cnt)

    l = 0
    for i in range(1, 8):
        if i == len(str(n - i)):
            l = n - i
            break;

    for s in b, str(l):
        for i in map(ord, s):
            cnt[i] -= 1

    res = ["".join([b] + [chr(k) * v for k, v in enumerate(cnt) if v > 0 ])] if b[0] > "0" else []

    for i in range(ord("1"), ord("9") + 1):
        if cnt[i] > 0:
            cnt[i] -= 1
            others = [chr(k) * v for k, v in enumerate(cnt) if v > 0]
            others.append(b)
            res.append("".join([chr(i)] + sorted(others)))
            break

    print(min(res))

def __starting_point():
    main()
__starting_point()

import sys

def main():
    a = sys.stdin.readline().strip()
    b = sys.stdin.readline().strip()

    if a == "01" or a == "10":
        print("0")
        return

    cnt = [0] * 256
    for i in map(ord, a):
        cnt[i] += 1
    n = sum(cnt)

    l = 0
    for i in range(1, 8):
        if i == len(str(n - i)):
            l = n - i
            break;

    for s in b, str(l):
        for i in map(ord, s):
            cnt[i] -= 1

    res = ["".join([b] + [chr(k) * v for k, v in enumerate(cnt) if v > 0 ])] if b[0] > "0" else []

    for i in range(ord("1"), ord("9") + 1):
        if cnt[i] > 0:
            cnt[i] -= 1
            others = [chr(k) * v for k, v in enumerate(cnt) if v > 0]
            others.append(b)
            res.append("".join([chr(i)] + sorted(others)))
            break

    print(min(res))

def __starting_point():
    main()
__starting_point()

a = input()
b = input()
d = [0] * 10
n = 0
for j in range(1000100):
    t = str(j)
    if len(t) + j == len(a):
        n = j
for x in str(n):
    d[ord(x) - ord('0')] -= 1
for x in a:
    d[ord(x) - ord('0')] += 1
for x in b:
    d[ord(x) - ord('0')] -= 1
if sum(d)==0:
    print(b)
else:
    A = []
    B = []
    C = []
    D = []
    if b[0] != '0':
        A = list(b)
        for j in range(10):
            for k in range(d[j]):
                A.append(chr(ord('0') + j))
    t = 1
    while t < len(d) and d[t] == 0:
        t += 1
    oo = ord('0')
    if t < len(d):
        B.append(chr(oo+t))
        d[t] -= 1
        for j in range(ord(b[0]) - oo):
            for k in range(d[j]):
                B.append(chr(ord('0') + j))
        B.extend(list(b))
        for j in range(ord(b[0]) - oo, 10):
            for k in range(d[j]):
                B.append(chr(ord('0') + j))
        
        
        C.append(chr(oo+t))
        for j in range(min(ord(b[0]) - oo+1, 10)):
            for k in range(d[j]):
                C.append(chr(ord('0') + j))
        C.extend(list(b))
        for j in range(ord(b[0]) - oo+1, 10):
            for k in range(d[j]):
                C.append(chr(ord('0') + j))
        
    ans = []
    if len(A) > 0:
        ans.append(''.join(A))
    
    if len(B) > 0:
        ans.append(''.join(B))
    
    if len(C) > 0:
        ans.append(''.join(C))
    
    print(min(ans))