#include <bits/stdc++.h>
using namespace std;
const int INF = 1e9 + 7;
string a, b;
int main() {
  cin >> a >> b;
  if (a.size() != b.size()) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    cout << a << endl;
  } else {
    string aa = a, bb = b;
    sort(aa.begin(), aa.end());
    sort(bb.begin(), bb.end());
    if (aa == bb) {
      cout << b << endl;
      return 0;
    }
    for (int i = a.size() - 1; i >= 0; --i) {
      string na = a;
      string ans = "";
      bool f = true;
      for (int j = (0); j < (i); ++j) {
        int pos = -1;
        for (int k = (0); k < (na.size()); ++k)
          if (na[k] == b[j]) {
            pos = k;
            break;
          }
        if (pos == -1) {
          f = false;
          break;
        }
        na.erase(na.begin() + pos);
        ans += b[j];
      }
      if (!f) continue;
      int mx = -1;
      for (int j = (0); j < (na.size()); ++j)
        if (na[j] < b[i]) {
          if (mx == -1 || na[mx] < na[j]) mx = j;
        }
      if (mx == -1) continue;
      ans += na[mx];
      na.erase(na.begin() + mx);
      sort(na.begin(), na.end());
      reverse(na.begin(), na.end());
      ans += na;
      cout << ans << endl;
      return 0;
    }
  }
  return 0;
}
