#include <bits/stdc++.h>
using namespace std;
bool used[20];
string get_num(string a, string b, int minimize) {
  for (int i = 0; i < 20; i++) used[i] = false;
  string num = "";
  bool to_max = false;
  if (b.length() > a.length()) to_max = true;
  for (int i = 0; i < (int)b.length(); i++) {
    char max = '0';
    int idx = -1;
    for (int j = 0; j < (int)a.length(); j++) {
      if (i == minimize) {
        if (!used[j] && a[j] >= max && a[j] < b[i]) {
          idx = j;
          max = a[j];
        }
      } else if ((to_max && !used[j] && a[j] >= max) ||
                 (!used[j] && a[j] >= max && a[j] <= b[i])) {
        idx = j;
        max = a[j];
      }
    }
    if (idx == -1) {
      for (int j = 0; j < (int)a.length(); j++) {
        if (!used[j]) {
          idx = j;
          max = a[j];
        }
      }
    }
    used[idx] = true;
    if (!to_max && max < b[i]) to_max = true;
    num += max;
    if (num.length() == a.length()) break;
  }
  return num;
}
int main(void) {
  string a, b;
  cin >> a >> b;
  string ans = "";
  for (int i = 0; i <= (int)b.length(); i++) {
    string tmp = get_num(a, b, i);
    if ((tmp.length() < b.length() || tmp <= b) && (ans == "" || tmp > ans))
      ans = tmp;
  }
  cout << ans;
  return 0;
}
