#include <bits/stdc++.h>
using namespace std;
char a[20], b[20], res[20];
int n, m;
inline bool judge() {
  int pos = n;
  for (int i = 1; i <= n; ++i) {
    if (a[i] != '-') {
      res[pos] = a[i];
      pos--;
    }
  }
  return (strcmp(res + 1, b + 1) <= 0);
}
int main() {
  scanf("%s", a + 1);
  scanf("%s", b + 1);
  n = strlen(a + 1);
  m = strlen(b + 1);
  sort(a + 1, a + n + 1, greater<char>());
  if (m > n) return puts(a + 1), 0;
  for (int i = 1; i <= m; ++i) res[i] = '0';
  char ch;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (a[j] == '-')
        continue;
      else {
        ch = res[i] = a[j];
        a[j] = '-';
        if (judge())
          break;
        else
          a[j] = ch;
      }
  puts(res + 1);
  return 0;
}
