#include <bits/stdc++.h>
using namespace std;
string a, b;
int main() {
  while (cin >> a >> b) {
    if (a.length() < b.length()) {
      sort(a.begin(), a.end(), greater<char>());
      cout << a << endl;
      continue;
    }
    multiset<char> my(a.begin(), a.end());
    if (my == multiset<char>(b.begin(), b.end())) {
      cout << b << endl;
      continue;
    }
    string prefix = "", ans = "";
    for (int i = 0; i < (int)b.length(); i++) {
      multiset<char>::iterator it = my.lower_bound(b[i]);
      if (it != my.begin()) {
        it--;
        char x = (*it);
        my.erase(it);
        string suffix(my.begin(), my.end());
        sort(suffix.begin(), suffix.end(), greater<char>());
        ans = max(ans, prefix + x + suffix);
        my.insert(x);
      }
      if (my.find(b[i]) != my.end()) {
        prefix += b[i];
        my.erase(my.find(b[i]));
      } else
        break;
    }
    cout << ans << endl;
  }
  return 0;
}
