#include <bits/stdc++.h>
using namespace std;
const double PI = acos(-1);
template <typename T>
T getint() {
  T val = 0;
  char c;
  while ((c = getchar()) && !(c >= '0' && c <= '9'))
    ;
  do {
    val = (val * 10) + c - '0';
  } while ((c = getchar()) && (c >= '0' && c <= '9'));
  return val;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  string a, b;
  cin >> a >> b;
  if ((int)(a).size() < (int)(b).size()) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    cout << a << "\n";
    return 0;
  }
  string marad = a;
  string akt;
  string ans((int)(a).size(), '0');
  for (int i = 0; i < (int)(a).size(); ++i) {
    bool van = false;
    for (int j = 0; j < (int)(marad).size(); ++j) {
      if (b[i] > marad[j]) {
        if (marad[j] == '0' && i == 0) continue;
        char tmp = marad[j];
        string cpy = marad;
        cpy.erase(find((cpy).begin(), (cpy).end(), tmp));
        sort((cpy).begin(), (cpy).end());
        reverse((cpy).begin(), (cpy).end());
        string curr = akt + tmp + cpy;
        ans = max(ans, curr);
      } else if (b[i] == marad[j]) {
        van = true;
      }
    }
    if (van) {
      marad.erase(find((marad).begin(), (marad).end(), b[i]));
      akt.push_back(b[i]);
      string cpy = marad;
      sort(cpy.begin(), cpy.end());
      string curr = akt + cpy;
      if (curr <= b) {
        ans = max(ans, curr);
      }
    } else
      break;
  }
  cout << ans << "\n";
  return 0;
  return 0;
}
