#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, pos, l, r, b, i, j;
  string cad, cad2, cad3, cad4, resp;
  cin >> cad >> cad2;
  sort(cad.begin(), cad.end());
  pos = cad.size();
  if (pos == cad2.size()) {
    for (i = 0; i < pos; i++) {
      cad4.clear();
      for (j = i + 1; j < cad2.size(); j++) {
        cad4 = cad4 + cad2[j];
      }
      b = cad.size();
      for (j = 0; j < b; j++) {
        cad3.clear();
        for (r = 0; r < j; r++) {
          cad3 = cad3 + cad[r];
        }
        for (r = j + 1; r < b; r++) {
          cad3 = cad3 + cad[r];
        }
        if (cad3 > cad4 && cad[j] == cad2[i]) {
          j--;
          break;
        } else {
          if (cad[j] > cad2[i]) {
            j--;
            break;
          }
        }
      }
      if (j == b) j--;
      cad3.clear();
      for (r = 0; r < j; r++) {
        cad3 = cad3 + cad[r];
      }
      for (r = j + 1; r < b; r++) {
        cad3 = cad3 + cad[r];
      }
      resp = resp + cad[j];
      if (cad[j] < cad2[i]) {
        for (r = b - 1; r > j; r--) {
          resp = resp + cad[r];
        }
        for (r = j - 1; r >= 0; r--) {
          resp = resp + cad[r];
        }
        cout << resp;
        return 0;
      }
      cad.clear();
      cad = cad3;
    }
  } else {
    for (j = cad.size() - 1; j >= 0; j--) {
      resp = resp + cad[j];
    }
  }
  cout << resp;
}
