#include <bits/stdc++.h>
using namespace std;
char a[20], b[20], c[20];
int main() {
  memset(a, 0, sizeof(a));
  memset(b, 0, sizeof(b));
  while (~scanf("%s%s", a, b)) {
    int aa, bb;
    aa = strlen(a);
    bb = strlen(b);
    sort(a, a + aa);
    if (aa < bb) {
      for (int i = aa - 1; i >= 0; i--) {
        printf("%c", a[i]);
      }
      printf("\n");
      continue;
    }
    strcpy(c, a);
    for (int i = 0; i < aa; i++) {
      strcpy(a, c);
      for (int j = i; j < aa; j++) {
        swap(a[i], a[j]);
        if (strcmp(a, b) != 1) {
          strcpy(c, a);
        }
      }
    }
    puts(a);
    memset(a, 0, sizeof(a));
    memset(b, 0, sizeof(b));
  }
  return 0;
}
