#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[22], b[22];
int f[22][262144];
int getbit(int x, int i) { return x >> i & 1; }
int onbit(int x, int i) { return x | (1 << i); }
int cal(int i, int x) {
  if (f[i][x] != -1) return f[i][x];
  if (i > n) return f[i][x] = 1;
  f[i][x] = 0;
  for (int j = 1; j <= n; ++j)
    if (!getbit(x, j - 1) && a[j] <= b[i]) {
      if (a[j] < b[i])
        f[i][x] = 1;
      else
        f[i][x] = cal(i + 1, onbit(x, j - 1));
      if (f[i][x]) break;
    }
  return f[i][x];
}
void trace(int i, int x) {
  if (i > n) return;
  for (int j = 1; j <= n; ++j)
    if (!getbit(x, j - 1) && a[j] <= b[i]) {
      if (a[j] < b[i]) {
        cout << a[j];
        for (int k = 1; k <= n; ++k)
          if (!getbit(x, k - 1) && k != j) cout << a[k];
        return;
      } else if (f[i + 1][onbit(x, j - 1)] == 1) {
        cout << a[j];
        trace(i + 1, onbit(x, j - 1));
        return;
      }
    }
}
int main() {
  scanf("%s%s", &a[1], &b[1]);
  n = strlen(&a[1]);
  m = strlen(&b[1]);
  sort(a + 1, a + n + 1, greater<char>());
  if (n < m) {
    for (int i = 1; i <= n; ++i) cout << a[i];
    return 0;
  }
  memset(f, -1, sizeof(f));
  cal(1, 0);
  trace(1, 0);
}
