#include <bits/stdc++.h>
using namespace std;
string a, b;
long long ac[10] = {0}, bc[10] = {0};
long long la, lb;
string func(long long ind, bool flag, long long c[10]) {
  if (ind == la) return "";
  long long lmt = (flag ? b[ind] - '0' : 9);
  string str = "";
  long long i;
  for (i = lmt; i >= 0; i--) {
    if (!c[i]) continue;
    c[i]--;
    bool nflag;
    if (flag) {
      if (i == lmt)
        nflag = true;
      else
        nflag = false;
    } else
      nflag = false;
    str = func(ind + 1, nflag, c);
    c[i]++;
    if (str.length() == (la - ind - 1)) break;
    str = "";
  }
  char ch = i + '0';
  if (c[i])
    str = ch + str;
  else
    str = "";
  return str;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin >> a >> b;
  la = a.length(), lb = b.length();
  for (long long i = 0; i < la; i++) ac[a[i] - '0']++;
  for (long long i = 0; i < lb; i++) bc[b[i] - '0']++;
  string str = "";
  if (la < lb) {
    for (long long i = 0; i < la; i++) {
      for (long long j = 9; j >= 0; j--) {
        if (!ac[j]) continue;
        ac[j]--;
        str += (j + '0');
        break;
      }
    }
    cout << str << endl;
    return 0;
  }
  str = func(0, true, ac);
  cout << str << endl;
}
