#include <bits/stdc++.h>
using namespace std;

string itos(int n){
	stringstream ss;
	ss << n;
	return ss.str();
}

int solve(string s,string t){

	if( s == "01" ){
		cout << "01" << endl;
		return 0;
	}
	assert( s.size() != 3 or count(s.begin(),s.end(),'0') <= 1 );

	vector<int> num(10);

	for( auto c : s ) num[c-'0']++;
	for( auto c : t ) num[c-'0']--;

	for(int i = 1 ; i <= s.size() ; i++){
		auto naive = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string ppp = t;
			for(int i = 0 ; i < tmp.size() ; i++){
				ppp += string(tmp[i],'0'+i);
			}
			sort(ppp.begin(),ppp.end());
			string good = "~";
			do{
				if( ppp.find(t) != -1 and ppp[0] != '0' ) good = min(good,ppp);
			}while( next_permutation(ppp.begin(), ppp.end()) );

			return good == "~" ? string("") : good;
		};
		auto f2 = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string res;
			int mx = t[0] - '0';
			// for(auto c : t ) {
				// mx = max<int>(c-'0',mx);
			// }
			for(int i = 0 ; i < mx ; i++){
				res += string(tmp[i],'0'+i);
			}
			int l = res.size();
			res += t;
			int r = res.size();
			for(int i = mx ; i < 10 ; i++){
				res += string(tmp[i],'0'+i);
			}
			// cout << res << endl;
			if( res[0] == '0' ){
				string another_best = "~";
				for(int i = 1 ; i < res.size() ; i++){
					if(	l <= i and i < r ){
						if( l == i and res[i] != '0' ){
							string copy = res;
							copy.erase(copy.begin()+l,copy.begin()+r);
							// cout << copy << endl;
							another_best = t + copy;
						}
						continue;
					}
					if( res[i] != '0' ){
						char c = res[i];
						res.erase(res.begin()+i);
						return min(another_best,string(1,c) + res);
					}
				}
				return another_best;
			}
			return res;

		};
		auto f = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string res;
			int mx = t[0] - '0' + 1;
			// for(auto c : t ) {
				// mx = max<int>(c-'0',mx);
			// }
			for(int i = 0 ; i < mx ; i++){
				res += string(tmp[i],'0'+i);
			}
			int l = res.size();
			res += t;
			int r = res.size();
			for(int i = mx ; i < 10 ; i++){
				res += string(tmp[i],'0'+i);
			}
			// cout << res << endl;
			if( res[0] == '0' ){
				string another_best = "~";
				for(int i = 0 ; i < res.size() ; i++){
					if(	l <= i and i < r ){
						if( l == i and res[i] != '0' ){
							string copy = res;
							copy.erase(copy.begin()+l,copy.begin()+r);
							// cout << copy << endl;
							another_best = t + copy;
							// cout << another_best << endl;
						}
						continue;
					}
					if( res[i] != '0' ){
						char c = res[i];
						res.erase(res.begin()+i);

						return min({another_best,string(1,c) + res,f2(d)});
					}
				}
				return min(another_best,f2(d));
			}
			return min(res,f2(d));

		};

		auto res = f(i);
		if( res != "" ){
			cout << res << endl;
			if( res != naive(i) ){
				cout << "[" << s << "," << t << "]" << endl;
				cout << res << endl;
				cout << naive(i) << "<" << endl;
				cout << endl;
				return 1;
			}
			return 0;
			//return 0;
		}
	}
	return -1;
}

int main(){
	string s,t;
	cin >> s >> t;
	solve(s,t);
	return 0;
	while(true){
		s = "";
		for(int i = 0 ; i < 3 ; i++) s += rand() % (10-!s.size()) + (s.size()?'0':'1');

		// cout << s << endl;
		int a = rand() % s.size();
		int b = rand() % s.size();
		if( a > b ) swap(a,b);
		t = s.substr(a,b-a+1);

		s += itos(s.size());
		random_shuffle(s.begin(),s.end());
		if( solve(s,t) ){
			cout << s << " " << t << endl;
			return 0;
		}

	}
}


#include <bits/stdc++.h>
using namespace std;

string itos(int n){
	stringstream ss;
	ss << n;
	return ss.str();
}

int solve(string s,string t){
	if( s == "01" ){
		cout << "0" << endl;
		return 0;
	}

	vector<int> num(10);

	for( auto c : s ) num[c-'0']++;
	for( auto c : t ) num[c-'0']--;

	for(int i = 1 ; i <= s.size() ; i++){
		auto naive = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string ppp = t;
			for(int i = 0 ; i < tmp.size() ; i++){
				ppp += string(tmp[i],'0'+i);
			}
			sort(ppp.begin(),ppp.end());
			string good = "~";
			do{
				if( ppp.find(t) != -1 and ppp[0] != '0' ) good = min(good,ppp);
			}while( next_permutation(ppp.begin(), ppp.end()) );

			return good == "~" ? string("") : good;
		};
		auto f2 = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string res;
			int mx = t[0] - '0';
			// for(auto c : t ) {
				// mx = max<int>(c-'0',mx);
			// }
			for(int i = 0 ; i < mx ; i++){
				res += string(tmp[i],'0'+i);
			}
			int l = res.size();
			res += t;
			int r = res.size();
			for(int i = mx ; i < 10 ; i++){
				res += string(tmp[i],'0'+i);
			}
			// cout << res << endl;
			if( res[0] == '0' ){
				string another_best = "~";
				for(int i = 1 ; i < res.size() ; i++){
					if(	l <= i and i < r ){
						if( l == i and res[i] != '0' ){
							string copy = res;
							copy.erase(copy.begin()+l,copy.begin()+r);
							// cout << copy << endl;
							another_best = t + copy;
						}
						continue;
					}
					if( res[i] != '0' ){
						char c = res[i];
						res.erase(res.begin()+i);
						return min(another_best,string(1,c) + res);
					}
				}
				return another_best;
			}
			return res;

		};
		auto f = [&](int d){
			vector<int> tmp = num;
			int j = d;
			string last;
			while(j) {
				last += '0' + j % 10;
				tmp[j%10]--;
				if( tmp[j%10] < 0 ) return string("");
				j /= 10;
			}
			int sum = t.size();
			for( auto x : tmp ) sum += x;
			if( sum != d ) return string("");
			reverse(last.begin(),last.end());

			string res;
			int mx = t[0] - '0' + 1;
			// for(auto c : t ) {
				// mx = max<int>(c-'0',mx);
			// }
			for(int i = 0 ; i < mx ; i++){
				res += string(tmp[i],'0'+i);
			}
			int l = res.size();
			res += t;
			int r = res.size();
			for(int i = mx ; i < 10 ; i++){
				res += string(tmp[i],'0'+i);
			}
			// cout << res << endl;
			if( res[0] == '0' ){
				string another_best = "~";
				for(int i = 0 ; i < res.size() ; i++){
					if(	l <= i and i < r ){
						if( l == i and res[i] != '0' ){
							string copy = res;
							copy.erase(copy.begin()+l,copy.begin()+r);
							// cout << copy << endl;
							another_best = t + copy;
							// cout << another_best << endl;
						}
						continue;
					}
					if( res[i] != '0' ){
						char c = res[i];
						res.erase(res.begin()+i);

						return min({another_best,string(1,c) + res,f2(d)});
					}
				}
				return min(another_best,f2(d));
			}
			return min(res,f2(d));

		};

		auto res = f(i);
		if( res != "" ){
			cout << res << endl;
			if( res != naive(i) ){
				cout << "[" << s << "," << t << "]" << endl;
				cout << res << endl;
				cout << naive(i) << "<" << endl;
				cout << endl;
				return 1;
			}
			return 0;
			//return 0;
		}
	}
	return -1;
}

int main(){
	string s,t;
	cin >> s >> t;
	solve(s,t);
	return 0;
	while(true){
		s = "";
		for(int i = 0 ; i < 3 ; i++) s += rand() % (10-!s.size()) + (s.size()?'0':'1');

		// cout << s << endl;
		int a = rand() % s.size();
		int b = rand() % s.size();
		if( a > b ) swap(a,b);
		t = s.substr(a,b-a+1);

		s += itos(s.size());
		random_shuffle(s.begin(),s.end());
		if( solve(s,t) ){
			cout << s << " " << t << endl;
			return 0;
		}

	}
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector<int> v;
const int N = 1001000;
int sum[N], sz[N], pos[N], suff[N], nsuff[N], head[N], col[N], ncol[N], cn = 0, lcp[N], p[N];

bool cmp(int i, int j) {
    return v[i] < v[j];
}

void suffix_array() {
    for (int i = 0; i < t.size(); i++)
        v.pb(t[i]);
    v.pb(-1);
    int n = v.size();
    for(int i = 0; i < n; i++) suff[i] = i;
    stable_sort(suff, suff + n, cmp);
    for(int i = 0; i < n; i++) {
        if(!i || v[suff[i]] != v[suff[i - 1]])
            col[suff[i]] = cn++, head[cn - 1] = i;
        else col[suff[i]] = cn - 1;
    }
    for(int len = 1; len < n; len += len) {
        for(int i = 0; i < n; i++) {
            int j = suff[i] - len;
            if(j < 0) j += n;
            nsuff[head[col[j]]++] = j;
        }
        cn = 0;
        for(int i = 0; i < n; i++) {
            suff[i] = nsuff[i];
            if(!i || col[suff[i]] != col[suff[i - 1]] || col[(suff[i] + len) % n] != col[(suff[i - 1] + len) % n])
                ncol[suff[i]] = cn++, head[cn - 1] = i;
            else ncol[suff[i]] = cn - 1;
        }
        for(int i = 0; i < n; i++) col[i] = ncol[i];
    }
    int len = n;
    n--;
    for (int i = 0; i < n; i++)
        suff[i] = suff[i + 1];
    for(int i = 0; i < n; i++) p[suff[i]] = i;
    int m = 0;
    for(int i = 0; i < n; i++) {
        int j = p[i];
        if(j == n - 1) {
            lcp[j] = 0;
            m = 0;
            continue;
        }
        m = max(m - 1, 0);
        while(i + m < n && suff[j + 1] + m < n && t[i + m] == t[suff[j + 1] + m]) m++;
        lcp[j] = m;
    }
}

int two[1001000];
int dp[21][1001000];

void precalc() {
    two[0] = -1;
    for (int i = 1; i < 1001000; i++) {
        two[i] = 1 + two[i / 2];
    }
    for (int i = 0; i < t.size(); i++) {
        dp[0][i] = lcp[i];
    }
    for (int lvl = 1; (1 << lvl) <= t.size(); lvl++) {
        for (int i = 0; i + (1 << lvl) <= t.size(); i++) {
            dp[lvl][i] = min(dp[lvl - 1][i], dp[lvl - 1][i + (1 << (lvl - 1))]);
        }
    }
}

int get(int l, int r) {
    if (l > r) return inf;
    int lvl = two[r - l + 1];
    int ans1 = dp[lvl][l];
    int ans2 = dp[lvl][r - (1 << lvl) + 1];
    return min(ans1, ans2);
}

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    int p1 = p[ind1];
    int p2 = p[ind2];
    if (p1 == p2) return 0;
    if (p1 > p2) swap(p1, p2);
    int x = get(p1, p2 - 1);
    if (x == min(t.size() - ind1, t.size() - ind2))
        return 0;
    if (t[ind1 + x] < t[ind2 + x])
        return -1;
    return 1;
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
}

int main(){

    getline(cin, s);
    getline(cin, t);

    suffix_array();

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    vector <int> A((int)2e6, 0);
    for (int i = 1; i <= (int)1e6; i++) {
        string dd = tostring(i);
        int x = i + dd.size();
        cout << x << endl;
        A[x]++;
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            cout << t;
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector<int> v;
const int N = 1001000;
int sum[N], sz[N], pos[N], suff[N], nsuff[N], head[N], col[N], ncol[N], cn = 0, lcp[N], p[N];

bool cmp(int i, int j) {
    return v[i] < v[j];
}

void suffix_array() {
    for (int i = 0; i < t.size(); i++)
        v.pb(t[i]);
    v.pb(-1);
    int n = v.size();
    for(int i = 0; i < n; i++) suff[i] = i;
    stable_sort(suff, suff + n, cmp);
    for(int i = 0; i < n; i++) {
        if(!i || v[suff[i]] != v[suff[i - 1]])
            col[suff[i]] = cn++, head[cn - 1] = i;
        else col[suff[i]] = cn - 1;
    }
    for(int len = 1; len < n; len += len) {
        for(int i = 0; i < n; i++) {
            int j = suff[i] - len;
            if(j < 0) j += n;
            nsuff[head[col[j]]++] = j;
        }
        cn = 0;
        for(int i = 0; i < n; i++) {
            suff[i] = nsuff[i];
            if(!i || col[suff[i]] != col[suff[i - 1]] || col[(suff[i] + len) % n] != col[(suff[i - 1] + len) % n])
                ncol[suff[i]] = cn++, head[cn - 1] = i;
            else ncol[suff[i]] = cn - 1;
        }
        for(int i = 0; i < n; i++) col[i] = ncol[i];
    }
    int len = n;
    n--;
    for (int i = 0; i < n; i++)
        suff[i] = suff[i + 1];
    for(int i = 0; i < n; i++) p[suff[i]] = i;
    int m = 0;
    for(int i = 0; i < n; i++) {
        int j = p[i];
        if(j == n - 1) {
            lcp[j] = 0;
            m = 0;
            continue;
        }
        m = max(m - 1, 0);
        while(i + m < n && suff[j + 1] + m < n && t[i + m] == t[suff[j + 1] + m]) m++;
        lcp[j] = m;
    }
}

int two[1001000];
int dp[21][1001000];

void precalc() {
    two[0] = -1;
    for (int i = 1; i < 1001000; i++) {
        two[i] = 1 + two[i / 2];
    }
    for (int i = 0; i < t.size(); i++) {
        dp[0][i] = lcp[i];
    }
    for (int lvl = 1; (1 << lvl) <= t.size(); lvl++) {
        for (int i = 0; i + (1 << lvl) <= t.size(); i++) {
            dp[lvl][i] = min(dp[lvl - 1][i], dp[lvl - 1][i + (1 << (lvl - 1))]);
        }
    }
}

int get(int l, int r) {
    if (l > r) return inf;
    int lvl = two[r - l + 1];
    int ans1 = dp[lvl][l];
    int ans2 = dp[lvl][r - (1 << lvl) + 1];
    return min(ans1, ans2);
}

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    int p1 = p[ind1];
    int p2 = p[ind2];
    if (p1 == p2) return 0;
    if (p1 > p2) swap(p1, p2);
    int x = get(p1, p2 - 1);
    if (x == min(t.size() - ind1, t.size() - ind2))
        return 0;
    if (t[ind1 + x] < t[ind2 + x])
        return -1;
    return 1;
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    suffix_array();

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    vector <pii> ans;
    for (int i = 0; i < 10; i++)
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t)));
for (int i = sz(a) - 2; i >= 0; i--)
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    vector <pii> ans;
    for (int i = 0; i < 10; i++)
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t)));
for (int i = sz(a) - 2; i >= 0; i--)
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t));
for (int i = sz(a) - 2; i >= 0; i--)
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t));
for (int i = sz(a) - 2; i >= 0; i--
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector<int> v;
const int N = 1001000;
int sum[N], sz[N], pos[N], suff[N], nsuff[N], head[N], col[N], ncol[N], cn = 0, lcp[N], p[N];

bool cmp(int i, int j) {
    return v[i] < v[j];
}

void suffix_array() {
    for (int i = 0; i < t.size(); i++)
        v.pb(t[i]);
    v.pb(-1);
    int n = v.size();
    for(int i = 0; i < n; i++) suff[i] = i;
    stable_sort(suff, suff + n, cmp);
    for(int i = 0; i < n; i++) {
        if(!i || v[suff[i]] != v[suff[i - 1]])
            col[suff[i]] = cn++, head[cn - 1] = i;
        else col[suff[i]] = cn - 1;
    }
    for(int len = 1; len < n; len += len) {
        for(int i = 0; i < n; i++) {
            int j = suff[i] - len;
            if(j < 0) j += n;
            nsuff[head[col[j]]++] = j;
        }
        cn = 0;
        for(int i = 0; i < n; i++) {
            suff[i] = nsuff[i];
            if(!i || col[suff[i]] != col[suff[i - 1]] || col[(suff[i] + len) % n] != col[(suff[i - 1] + len) % n])
                ncol[suff[i]] = cn++, head[cn - 1] = i;
            else ncol[suff[i]] = cn - 1;
        }
        for(int i = 0; i < n; i++) col[i] = ncol[i];
    }
    int len = n;
    n--;
    for (int i = 0; i < n; i++)
        suff[i] = suff[i + 1];
    for(int i = 0; i < n; i++) p[suff[i]] = i;
    int m = 0;
    for(int i = 0; i < n; i++) {
        int j = p[i];
        if(j == n - 1) {
            lcp[j] = 0;
            m = 0;
            continue;
        }
        m = max(m - 1, 0);
        while(i + m < n && suff[j + 1] + m < n && t[i + m] == t[suff[j + 1] + m]) m++;
        lcp[j] = m;
    }
}

int two[1001000];
int dp[21][1001000];

void precalc() {
    two[0] = -1;
    for (int i = 1; i < 1001000; i++) {
        two[i] = 1 + two[i / 2];
    }
    for (int i = 0; i < t.size(); i++) {
        dp[0][i] = lcp[i];
    }
    for (int lvl = 1; (1 << lvl) <= t.size(); lvl++) {
        for (int i = 0; i + (1 << lvl) <= t.size(); i++) {
            dp[lvl][i] = min(dp[lvl - 1][i], dp[lvl - 1][i + (1 << (lvl - 1))]);
        }
    }
}

int get(int l, int r) {
    if (l > r) return inf;
    int lvl = two[r - l + 1];
    int ans1 = dp[lvl][l];
    int ans2 = dp[lvl][r - (1 << lvl) + 1];
    return min(ans1, ans2);
}

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    int p1 = p[ind1];
    int p2 = p[ind2];
    if (p1 == p2) return 0;
    if (p1 > p2) swap(p1, p2);
    int x = get(p1, p2 - 1);
    if (x == min(t.size() - ind1, t.size() - ind2))
        return 0;
    if (t[ind1 + x] < t[ind2 + x])
        return -1;
    return 1;
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
}

int main(){

    getline(cin, s);
    getline(cin, t);

    suffix_array();

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            cout << t;
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector<int> v;
const int N = 1001000;
int sum[N], sz[N], pos[N], suff[N], nsuff[N], head[N], col[N], ncol[N], cn = 0, lcp[N], p[N];

bool cmp(int i, int j) {
    return v[i] < v[j];
}

void suffix_array() {
    for (int i = 0; i < t.size(); i++)
        v.pb(t[i]);
    v.pb(-1);
    int n = v.size();
    for(int i = 0; i < n; i++) suff[i] = i;
    stable_sort(suff, suff + n, cmp);
    for(int i = 0; i < n; i++) {
        if(!i || v[suff[i]] != v[suff[i - 1]])
            col[suff[i]] = cn++, head[cn - 1] = i;
        else col[suff[i]] = cn - 1;
    }
    for(int len = 1; len < n; len += len) {
        for(int i = 0; i < n; i++) {
            int j = suff[i] - len;
            if(j < 0) j += n;
            nsuff[head[col[j]]++] = j;
        }
        cn = 0;
        for(int i = 0; i < n; i++) {
            suff[i] = nsuff[i];
            if(!i || col[suff[i]] != col[suff[i - 1]] || col[(suff[i] + len) % n] != col[(suff[i - 1] + len) % n])
                ncol[suff[i]] = cn++, head[cn - 1] = i;
            else ncol[suff[i]] = cn - 1;
        }
        for(int i = 0; i < n; i++) col[i] = ncol[i];
    }
    int len = n;
    n--;
    for (int i = 0; i < n; i++)
        suff[i] = suff[i + 1];
    for(int i = 0; i < n; i++) p[suff[i]] = i;
    int m = 0;
    for(int i = 0; i < n; i++) {
        int j = p[i];
        if(j == n - 1) {
            lcp[j] = 0;
            m = 0;
            continue;
        }
        m = max(m - 1, 0);
        while(i + m < n && suff[j + 1] + m < n && t[i + m] == t[suff[j + 1] + m]) m++;
        lcp[j] = m;
    }
}

int two[1001000];
int dp[21][1001000];

void precalc() {
    two[0] = -1;
    for (int i = 1; i < 1001000; i++) {
        two[i] = 1 + two[i / 2];
    }
    for (int i = 0; i < t.size(); i++) {
        dp[0][i] = lcp[i];
    }
    for (int lvl = 1; (1 << lvl) <= t.size(); lvl++) {
        for (int i = 0; i + (1 << lvl) <= t.size(); i++) {
            dp[lvl][i] = min(dp[lvl - 1][i], dp[lvl - 1][i + (1 << (lvl - 1))]);
        }
    }
}

int get(int l, int r) {
    if (l > r) return inf;
    int lvl = two[r - l + 1];
    int ans1 = dp[lvl][l];
    int ans2 = dp[lvl][r - (1 << lvl) + 1];
    return min(ans1, ans2);
}

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    vector <pii> ans;
    for (int i = 0; i < 10; i++)
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            if (l == a.size()) return -1;
            if (r == b.size()) return 1;
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t)));
for (int i = sz(a) - 2; i >= 0; i--)
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

vector <pii> get2(vector <int> cnt) {
    int st = -1;
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
a = get2(cur);
a.pb(mp(-1, sz(t)));
for (int i = sz(a) - 2; i >= 0; i--)
swap(a[i], a[i + 1]);
update(a);
}

int main(){

    getline(cin, s);
    getline(cin, t);

    if (s.size() == 2) {
        cout << t << endl;
        return 0;
    }

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


#pragma comment(linker, "/STACK:64000000")
#include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>
#include <ctime>
#include <sstream>
#include <algorithm>
#include <functional>
#include <numeric>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>

using namespace std;

#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define pii pair < int, int >
#define pll pair < long long, long long>
#define ull unsigned long long
#define y1 stupid_cmath
#define left stupid_left
#define right stupid_right
#define vi vector <int>
#define sz(a) (int)a.size()
#define forit(it, s) for(__typeof(s.begin()) it = s.begin(); it != s.end(); it++)
#define all(a) a.begin(), a.end()
#define sqr(x) ((x) * (x))

const int inf = (int)1e9;
const int mod = inf + 7;
const double eps = 1e-9;
const double pi = acos(-1.0);

string s, t;
vector <pii> ans;
vector <int> cnt, cntt;
int nxt[1001000];

vector<int> v;
const int N = 1001000;
int sum[N], sz[N], pos[N], suff[N], nsuff[N], head[N], col[N], ncol[N], cn = 0, lcp[N], p[N];

bool cmp(int i, int j) {
    return v[i] < v[j];
}

void suffix_array() {
    for (int i = 0; i < t.size(); i++)
        v.pb(t[i]);
    v.pb(-1);
    int n = v.size();
    for(int i = 0; i < n; i++) suff[i] = i;
    stable_sort(suff, suff + n, cmp);
    for(int i = 0; i < n; i++) {
        if(!i || v[suff[i]] != v[suff[i - 1]])
            col[suff[i]] = cn++, head[cn - 1] = i;
        else col[suff[i]] = cn - 1;
    }
    for(int len = 1; len < n; len += len) {
        for(int i = 0; i < n; i++) {
            int j = suff[i] - len;
            if(j < 0) j += n;
            nsuff[head[col[j]]++] = j;
        }
        cn = 0;
        for(int i = 0; i < n; i++) {
            suff[i] = nsuff[i];
            if(!i || col[suff[i]] != col[suff[i - 1]] || col[(suff[i] + len) % n] != col[(suff[i - 1] + len) % n])
                ncol[suff[i]] = cn++, head[cn - 1] = i;
            else ncol[suff[i]] = cn - 1;
        }
        for(int i = 0; i < n; i++) col[i] = ncol[i];
    }
    int len = n;
    n--;
    for (int i = 0; i < n; i++)
        suff[i] = suff[i + 1];
    for(int i = 0; i < n; i++) p[suff[i]] = i;
    int m = 0;
    for(int i = 0; i < n; i++) {
        int j = p[i];
        if(j == n - 1) {
            lcp[j] = 0;
            m = 0;
            continue;
        }
        m = max(m - 1, 0);
        while(i + m < n && suff[j + 1] + m < n && t[i + m] == t[suff[j + 1] + m]) m++;
        lcp[j] = m;
    }
}

int two[1001000];
int dp[21][1001000];

void precalc() {
    two[0] = -1;
    for (int i = 1; i < 1001000; i++) {
        two[i] = 1 + two[i / 2];
    }
    for (int i = 0; i < t.size(); i++) {
        dp[0][i] = lcp[i];
    }
    for (int lvl = 1; (1 << lvl) <= t.size(); lvl++) {
        for (int i = 0; i + (1 << lvl) <= t.size(); i++) {
            dp[lvl][i] = min(dp[lvl - 1][i], dp[lvl - 1][i + (1 << (lvl - 1))]);
        }
    }
}

int get(int l, int r) {
    if (l > r) return inf;
    int lvl = two[r - l + 1];
    int ans1 = dp[lvl][l];
    int ans2 = dp[lvl][r - (1 << lvl) + 1];
    return min(ans1, ans2);
}

vector <pii> get(vector <int> cnt) {
    int st = -1;
    for (int i = 1; i < 10; i++) {
        if (cnt[i]) {
            st = i;
            break;
        }
    }
    if (st == -1) {
        vector<pii> ans;
        ans.pb(mp(0, cnt[0]));
        return ans;
    }
    cnt[st]--;
    vector<pii> ans;
    ans.pb(mp(st, 1));
    for (int i = 0; i < 10; i++) {
        if (cnt[i])
            ans.pb(mp(i, cnt[i]));
    }
    return ans;
}

string tostring(int n) {
    string ans = "";
    while (n) {
        ans = (char)(n % 10 + '0') + ans;
        n /= 10;
    }
    return ans;
}

int get_len(vector <pii> &a) {
    int ans = 0;
    for (int i = 0; i < a.size(); i++)
        ans += a[i].s;
    return ans;
}

// 1 if [ind:) > ddd...ddd
int f(int ind, int d, int cnt) {
    assert(d != -1);
    if (t[ind] - '0' != d) {
        if (t[ind] - '0' > d) return 1;
        return -1;
    }
    if (nxt[ind] == -1) return 0;
    if (nxt[ind] - ind >= cnt) return 0;
    int c = t[nxt[ind]] - '0';
    if (c > d) return 1;
    return -1;
}

// -1 if [ind1:) < [ind2:)
int f(int ind1, int ind2) {
    int p1 = p[ind1];
    int p2 = p[ind2];
    if (p1 == p2) return 0;
    if (p1 > p2) swap(p1, p2);
    int x = get(p1, p2 - 1);
    if (x == min(t.size() - ind1, t.size() - ind2))
        return 0;
    if (t[ind1 + x] < t[ind2 + x])
        return -1;
    return 1;
    if (ind1 == t.size() || ind2 == t.size()) return 0;
    if (t[ind1] != t[ind2]) {
        if (t[ind1] < t[ind2]) return -1;
        return 1;
    }
    return f(ind1 + 1, ind2 + 1);
}

int cmp(vector <pii> a, vector <pii> b) {
    int l = 0, r = 0;
    int L = 0, R = 0;
    while (true) {
        if (l == a.size() && r == b.size())
            break;
        if (l == a.size() || r == b.size()) {
            assert(false);
            return -1;
        }
        if (a[l].f != -1 && b[r].f != -1) {
            if (a[l].f < b[r].f) return 1;
            if (a[l].f > b[r].f) return -1;
            if (a[l].s == b[r].s) {
                l++;
                r++;
            } else if (a[l].s < b[r].s) {
                b[r].s -= a[l].s;
                l++;
            } else {
                a[l].s -= b[r].s;
                r++;
            }
        } else if (a[l].f == -1 && b[r].f != -1) {
            int c = f(L, b[r].f, b[r].s);
            if (c == 1) return -1;
            if (c == -1) return 1;
            int len = t.size() - L;
            if (len == b[r].s) {
                r++;
                l++;
            } else if (len < b[r].s) {
                b[r].s -= len;
                l++;
            } else {
                L += b[r].s;
                r++;
            }
        } else if (a[l].f != -1 && b[r].f == -1) {
            int c = f(R, a[l].f, a[l].s);
            if (c == 1) return 1;
            if (c == -1) return -1;
            int len = t.size() - R;
            if (len == a[l].s) {
                l++;
                r++;
            } else if (len < a[l].s) {
                a[l].s -= len;
                r++;
            } else {
                R += a[l].s;
                l++;
            }
        } else {
            int c = f(L, R);
            if (c == -1) {
                return 1;
            }
            if (c == 1) {
                return -1;
            }
            if (L == R) {
                l++;
                r++;
            } else if (L < R) {
                L += t.size() - R;
                r++;
            } else {
                R += t.size() - L;
                l++;
            }
        }
    }
    return 0;
}

void update(vector <pii> a) {
    if (a[0].f == 0) return;
    if (a[0].f == -1 && t[0] == '0') return;
    if (get_len(a) > get_len(ans)) {
        return;
    }
    if (get_len(a) < get_len(ans)) {
        ans = a;
        return;
    }
    int c = cmp(ans, a);
    if (c == -1) ans = a;
}

void process(int len) {
    vector <int> cur = cnt;
    string dd = tostring(len);
    for (int i = 0; i < dd.size(); i++) {
        cur[dd[i] - '0']--;
    }
    for (int i = 0; i < 10; i++) {
        cur[i] -= cntt[i];
    }
    if (*min_element(all(cur)) < 0) return;
    vector <pii> a = get(cur);
    a.pb(mp(-1, sz(t)));
    assert(a.size() <= 12);
    update(a);
    for (int i = sz(a) - 2; i >= 0; i--) {
        swap(a[i], a[i + 1]);
        update(a);
    }
}

int main(){

    getline(cin, s);
    getline(cin, t);

    suffix_array();

    memset(nxt, -1, sizeof nxt);

    for (int i = sz(t) - 1; i >= 0; i--) {
        if (i == sz(t) - 1) nxt[i] = -1;
        else {
            if (t[i] != t[i + 1]) nxt[i] = i + 1;
            else nxt[i] = nxt[i + 1];
        }
    }

    ans.pb(mp(9, inf));

    cnt.assign(10, 0);
    cntt.assign(10, 0);

    for (int i = 0; i < sz(s); i++) {
        cnt[s[i] - '0']++;
    }
    for (int i = 0; i < sz(t); i++) {
        cntt[t[i] - '0']++;
    }

    for (int i = 1; i <= sz(s); i++) {
        string dd = tostring(i);
        if (i + sz(dd) == sz(s)) {
            process(i);
        }
    }

    for (int i = 0; i < ans.size(); i++) {
        if (ans[i].f == -1) {
            for (int j = 0; j < t.size(); j++)
                printf("%c", t[j]);
            continue;
        }
        while (ans[i].s) {
            printf("%d", ans[i].f);
            ans[i].s--;
        }
    }
    printf("\n");

    return 0;
}


//writed by dnvtmf
#include <bits/stdc++.h>
#define INF 1000000007
#define FI first
#define SE second
#define PB push_back
#define VI vector<int>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int NUM = 1000010;
char s[NUM], ss[NUM], ans[NUM];
int cnt[128];
int n, m;
int main()
{
#ifdef ACM_TEST
    freopen("in.txt", "r", stdin);
//  freopen("in.txt", "w", stdout);
#endif
    scanf("%s %s", s, ss);
    n = strlen(s);
    m = strlen(ss);
    for(int i = 0; i < n; ++i) ++cnt[s[i]];
    for(int i = 0; i < m; ++i) --cnt[ss[i]];
    int sum = 0;
    for(int i = '1'; i <= '9'; ++i)++sum;
    for(int i = 1; i < n; ++i)
    {
        int num[10] = {}, tot = 0;
        int x = i, tsum = sum;
        while(x)
        {
            ++tot;
            ++num[x % 10];
            x /= 10;
            if(x % 10)
                --tsum;
        }
        bool flag = (i + tot == n) && !(tsum == 0 && ss[0] == '0' && m != 1);
        for(int j = 0; flag && j < 10; ++j)
            if(num[j] > cnt[j + '0'])
            {
                flag = false;
            }
        if(flag)
        {
//            printf("n = %d\n", i);
            for(int j = 0; j < 10; ++j)
                cnt[j + '0'] -= num[j];
            break;
        }
    }
    n = 0;
    for(int c = '1'; c <= '9'; ++c)
    {
        if(cnt[c] > 0)
        {
            s[n++] = c;
            --cnt[c];
            break;
        }
    }
    if(n == 0)
    {
        for(int i = 0; i < cnt['0']; ++i) ss[m++] = '0';
        ss[m] = 0;
        puts(ss);
        return 0;
    }
    bool dec = true;
    for(int i = 1; i < m; ++i)
        if(ss[i] != ss[i - 1])
        {
            dec = ss[i] < ss[i - 1];
            break;
        }
    if(ss[0] == '0' || !dec)
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
        }
        s[n] = 0;
        puts(s);
        return 0;
    }
    else
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
        }
        s[n] = 0;
        puts(s);
        return 0;
    }
    return 0;
}


//writed by dnvtmf
#include <bits/stdc++.h>
#define INF 1000000007
#define FI first
#define SE second
#define PB push_back
#define VI vector<int>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int NUM = 1000010;
char s[NUM], ss[NUM], ans[NUM];
int cnt[128], sum;
int n, m;

void deal_length()
{
    for(int i = 1; i < n; ++i)
    {
        int num[10] = {}, tot = 0;
        int x = i, tsum = sum;
        while(x)
        {
            ++tot;
            ++num[x % 10];
            x /= 10;
            if(x % 10) --tsum;
        }
        bool flag = (i + tot == n) && !(tsum == 0 && ss[0] == '0' && m != 1);
        for(int j = 0; flag && j < 10; ++j)
            if(num[j] > cnt[j + '0'])
            {
                flag = false;
            }
        if(flag)
        {
//            printf("n = %d\n", i);
            for(int j = 0; j < 10; ++j)
                cnt[j + '0'] -= num[j];
            break;
        }
    }
}
int main()
{
#ifdef ACM_TEST
    freopen("in.txt", "r", stdin);
//  freopen("in.txt", "w", stdout);
#endif
    scanf("%s %s", s, ss);
    n = strlen(s);
    m = strlen(ss);
    sum = 0;
    for(int i = 0; i < n; ++i) ++cnt[s[i]];
    for(int i = 0; i < m; ++i) --cnt[ss[i]];
    for(int i = '1'; i <= '9'; ++i) ++sum;
    deal_length();
    bool dec = ss[0] != '0';
    for(int i = 1; i < m; ++i)
        if(ss[i] != ss[i - 1])
        {
            dec = ss[i] < ss[i - 1];
            break;
        }
    bool first = dec;
    for(int i = '1'; first && i < ss[0]; ++i)
        if(cnt[i])
        {
            first = false;
            break;
        }
    n = 0;
    if(sum == 0 || first)
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            while(cnt[c]--)
            {
                ss[m++] = c;
            }
        }
        ss[m] = 0;
        puts(ss);
        return 0;
    }
    for(int c = '1'; c <= '9'; ++c)
    {
        if(cnt[c] > 0)
        {
            s[n++] = c;
            --cnt[c];
            break;
        }
    }
    if(dec)
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
        }
    }
    else
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
        }
    }
    s[n] = 0;
    puts(s);
    return 0;
}


//writed by dnvtmf
#include <bits/stdc++.h>
#define INF 1000000007
#define FI first
#define SE second
#define PB push_back
#define VI vector<int>
using namespace std;
typedef long long LL;
typedef pair <int, int> P;
const int NUM = 1000010;
char s[NUM], ss[NUM], ans[NUM];
int cnt[128];
int n, m;
int main()
{
#ifdef ACM_TEST
    freopen("in.txt", "r", stdin);
//  freopen("in.txt", "w", stdout);
#endif
    scanf("%s %s", s, ss);
    n = strlen(s);
    m = strlen(ss);
    for(int i = 0; i < n; ++i) ++cnt[s[i]];
    for(int i = 0; i < m; ++i) --cnt[ss[i]];
    int sum = 0;
    for(int i = '1'; i <= '9'; ++i) ++sum;
    for(int i = 1; i < n; ++i)
    {
        int num[10] = {}, tot = 0;
        int x = i, tsum = sum;
        while(x)
        {
            ++tot;
            ++num[x % 10];
            x /= 10;
            if(x % 10)
                --tsum;
        }
        bool flag = (i + tot == n) && !(tsum == 0 && ss[0] == '0' && m != 1);
        for(int j = 0; flag && j < 10; ++j)
            if(num[j] > cnt[j + '0'])
            {
                flag = false;
            }
        if(flag)
        {
//            printf("n = %d\n", i);
            for(int j = 0; j < 10; ++j)
                cnt[j + '0'] -= num[j];
            break;
        }
    }
    bool dec = ss[0] != '0';
    for(int i = 1; i < m; ++i)
        if(ss[i] != ss[i - 1])
        {
            dec = ss[i] < ss[i - 1];
            break;
        }
    bool first = dec;
    for(int i = '1'; first && i < ss[0]; ++i)
        if(cnt[i])
        {
            first = false;
            break;
        }
    n = 0;
    if(sum == 0 || first)
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            while(cnt[c]--)
            {
                ss[m++] = c;
            }
        }
        puts(ss);
        return 0;
    }
    for(int c = '1'; c <= '9'; ++c)
    {
        if(cnt[c] > 0)
        {
            s[n++] = c;
            --cnt[c];
            break;
        }
    }
    if(dec)
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
        }
    }
    else
    {
        for(int c = '0'; c <= '9'; ++c)
        {
            for(int i = 0; i < cnt[c]; ++i)
            {
                s[n++] = c;
            }
            if(c == ss[0])
            {
                for(int i = 0; i < m; ++i)
                {
                    s[n++] = ss[i];
                }
            }
        }
    }
    s[n] = 0;
    puts(s);
    return 0;
}