#include <bits/stdc++.h>
using namespace std;
struct lastHaveSmaller {
  int smallerIndx;
  string aCopy;
  string resCopy;
  lastHaveSmaller() {
    smallerIndx = 0;
    aCopy = "";
    resCopy = "";
  }
};
bool f(char a, char b) { return a > b; }
int main() {
  string a, b, res = "";
  cin >> a >> b;
  lastHaveSmaller LHS;
  sort(a.begin(), a.end(), f);
  if (a.length() < b.length()) {
    cout << a;
    return 0;
  }
  for (int i = 0; i < b.length(); i++) {
    int j = -1;
    for (int k = 0; k < a.length(); k++) {
      if (a[k] <= b[i]) {
        j = k;
        break;
      }
    }
    if (j == -1) {
      res = LHS.resCopy;
      a = LHS.aCopy;
      res += a[LHS.smallerIndx];
      a.erase(a.begin() + LHS.smallerIndx);
      res += a;
      cout << res;
      return 0;
    } else if (a[j] == b[i]) {
      int smaller = -1;
      for (int k = 0; k < a.length(); k++) {
        if (a[k] < b[i]) {
          smaller = k;
          break;
        }
      }
      if (smaller != -1) {
        LHS.aCopy = a;
        LHS.resCopy = res;
        LHS.smallerIndx = smaller;
      }
      res += a[j];
      a.erase(a.begin() + j);
    } else if (a[j] < b[i]) {
      res += a[j];
      a.erase(a.begin() + j);
      res += a;
      cout << res;
      return 0;
    }
  }
  cout << res;
  return 0;
}
