#include <bits/stdc++.h>
using namespace std;
int n, pos, l, r;
int a[19];
int so[19];
int solen;
int ptr[19];
int b[19];
int alen, blen;
int f[19] = {0};
char sa[20];
char sb[20];
int answer[19];
void input() {
  scanf("%s%s", sa, sb);
  int i;
  for (i = 0; sa[i]; i++) {
    a[i] = sa[i] - '0';
    f[i] = 0;
  }
  alen = i;
  for (i = 0; sb[i]; i++) {
    b[i] = sb[i] - '0';
  }
  blen = i;
}
int bsearch(int d) {
  int s, e;
  int m;
  for (s = 0, e = solen - 1; s <= e;) {
    m = (s + e) / 2;
    if (so[m] == d) return m;
    if (so[m] > d) {
      e = m - 1;
    } else {
      s = m + 1;
    }
  }
  return e;
}
bool dfs(int bi, bool tie) {
  if (bi == alen) return true;
  int k = alen - 1;
  if (tie) {
    k = bsearch(b[bi]);
    if (k >= 0) k = ptr[k];
  }
  int j;
  for (j = k; j >= 0; j--) {
    if (f[j] == 0) {
      if (tie == true && a[j] != b[bi]) tie = false;
      answer[bi] = a[j];
      f[j] = 1;
      if (dfs(bi + 1, tie) == true) return true;
      f[j] = 0;
      answer[bi] = -1;
      if (tie) {
        for (j--; j >= 0; j--) {
          if ((a[j] < b[bi]) && !f[j]) {
            j++;
            tie = false;
            break;
          }
        }
      }
    }
  }
  return false;
}
void solution() {
  sort(a, a + alen);
  so[0] = a[0];
  ptr[0] = 0;
  solen = 1;
  for (int i = 1, j = 0; i < alen; i++) {
    if (a[i] != a[i - 1]) {
      so[++j] = a[i];
      solen++;
    }
    ptr[j] = i;
  }
  for (int i = 0; i < alen; i++) {
    answer[i] = -1;
  }
  if (alen == blen) {
    bool tie = true;
    dfs(0, tie);
  } else {
    bool tie = false;
    dfs(0, tie);
  }
}
int main(int argc, char **argv) {
  input();
  solution();
  bool nz = false;
  for (int i = 0; i < alen; i++) {
    if (answer[i] == -1) break;
    if (answer[i] == 0 && !nz) continue;
    if (answer[i] > 0 && !nz) nz = true;
    cout << answer[i];
  }
  cout << "\n";
  return 0;
}
