#include <bits/stdc++.h>
using namespace std;
unsigned long long a, inv, nr1, nr2;
int fr[11];
string b;
vector<int> res;
int main() {
  cin >> a >> b;
  while (a != 0) {
    nr1++;
    fr[a % 10]++;
    a /= 10;
  }
  if (b.size() > nr1) {
    for (int i = 9; i >= 0; i--)
      for (int j = 1; j <= fr[i]; j++) cout << i;
    return 0;
  }
  for (auto it : b) {
    int c = it - '0';
    if (fr[c] == 0) {
      int ok = 0;
      for (int i = c - 1; i >= 0 + (res.size() == 0); i--)
        if (fr[i] != 0) {
          res.push_back(i);
          fr[i]--;
          ok = 1;
          break;
        }
      if (ok) {
        for (auto it : res) cout << it;
        for (int i = 9; i >= 0; i--)
          for (int j = 1; j <= fr[i]; j++) cout << i;
        return 0;
      } else {
        while (true) {
          for (int i = res.back() - 1; i >= 0; i--) {
            if (fr[i] != 0) {
              fr[res.back()]++;
              res.back() = i;
              fr[i]--;
              ok = 1;
              break;
            }
          }
          if (ok) break;
          fr[res.back()]++;
          res.pop_back();
        }
        for (auto it : res) cout << it;
        for (int i = 9; i >= 0; i--)
          for (int j = 1; j <= fr[i]; j++) cout << i;
        return 0;
      }
    } else {
      res.push_back(c);
      fr[c]--;
    }
    inv /= 10;
  }
  for (auto it : res) cout << it;
  for (int i = 9; i >= 0; i--)
    for (int j = 1; j <= fr[i]; j++) cout << i;
  return 0;
}
