#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize(2)
#pragma G++ optimize(2)
const int INF = 0x3f3f3f3f;
const long long LINF = 1000000000000000000;
long long n, a[2000005], b[2000005], l1, l2;
char s[25], ss[25], sss[25];
void solve(int x) {
  for (int i = 1; i < x; i++) printf("%c", sss[i]);
  for (int i = 9; i >= 0; i--) {
    while (a[i]) {
      printf("%d", i);
      a[i]--;
    }
  }
  printf("\n");
  exit(0);
}
int main() {
  scanf("%s", s + 1);
  scanf("%s", ss + 1);
  l1 = strlen(s + 1), l2 = strlen(ss + 1);
  for (int i = 1; i <= l1; i++) a[s[i] - '0']++;
  for (int i = 1; i <= l2; i++) b[ss[i] - '0']++;
  if (l1 < l2) solve(1);
  for (int i = 1; i <= l1; i++) {
    if (a[ss[i] - '0']) {
      sss[i] = ss[i];
      a[ss[i] - '0']--;
    } else {
      for (int j = (ss[i] - '0') - 1; j >= 0; j--) {
        if (a[j]) {
          sss[i] = '0' + j;
          a[j]--;
          solve(i + 1);
        }
      }
      int k = i - 1;
      while (1) {
        a[ss[k] - '0']++;
        for (int j = (ss[k] - '0') - 1; j >= 0; j--) {
          if (a[j]) {
            sss[k] = '0' + j;
            a[j]--;
            solve(k + 1);
          }
        }
        k--;
      }
    }
  }
  solve(l1 + 1);
  return 0;
}
