#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 18;
const int INF = 1 << 30;
const long long LINF = 1LL << 60;
char s[MAX_N + 4], t[MAX_N + 4];
int cs[10];
int main() {
  scanf("%s%s", s, t);
  int n = strlen(s), m = strlen(t);
  if (n < m) {
    sort(s, s + n, greater<char>());
    puts(s);
    return 0;
  }
  for (int i = 0; i < n; i++) cs[s[i] - '0']++;
  int k = 0;
  for (; k < m; k++) {
    int tk = t[k] - '0';
    if (cs[tk] == 0) break;
    s[k] = t[k];
    cs[tk]--;
  }
  if (k >= m) {
    puts(s);
    return 0;
  }
  while (k >= 0) {
    bool f = false;
    for (int i = (t[k] - '0') - 1; i >= 0; i--)
      if (cs[i] > 0) {
        s[k++] = i + '0';
        cs[i]--;
        f = true;
        break;
      }
    if (f) break;
    cs[s[--k] - '0']++;
  }
  for (; k < m; k++) {
    for (int i = 9; i >= 0; i--)
      if (cs[i] > 0) {
        s[k] = i + '0';
        cs[i]--;
        break;
      }
  }
  puts(s);
  return 0;
}
