#include <bits/stdc++.h>
using namespace std;
int la, lb, cnt[20], ans[25];
char a[25], b[25];
void find(int v, bool flag) {
  if (v > lb) {
    for (int i = 1; i <= lb; i++) printf("%d", ans[i]);
    exit(0);
  }
  if (flag && cnt[b[v] - '0']) {
    cnt[b[v] - '0']--;
    ans[v] = b[v] - '0';
    find(v + 1, 1);
    ans[v] = 0;
    cnt[b[v] - '0']++;
  }
  for (int i = (flag ? b[v] - '0' - 1 : 9); i >= 0; i--)
    if (cnt[i]) {
      ans[v] = i;
      cnt[i]--;
      find(v + 1, 0);
      ans[v] = 0;
      cnt[i]++;
    }
}
int main() {
  scanf("%s%s", a + 1, b + 1);
  la = strlen(a + 1), lb = strlen(b + 1);
  for (int i = 1; i <= la; i++) cnt[a[i] - '0']++;
  if (lb > la) {
    lb = la;
    for (int i = 1; i <= lb; i++) b[i] = '9';
  }
  find(1, 1);
  return 0;
}
