#include <bits/stdc++.h>
using namespace std;
string st, sss;
int b[19];
int a[10], aa[19], d[19];
long long ans;
bool cmp(int x, int y) { return x > y; }
void dp(int n, int s[], long long ss, int c[], int f) {
  if ((unsigned int)n > st.size()) {
    ans = max(ans, ss);
    return;
  }
  int i;
  if (f == 1) {
    for (i = 9; i >= 0; i--) {
      if (c[i] != 0) {
        c[i]--;
        s[n] = i;
        dp(n + 1, s, ss * 10 + i, c, 1);
        s[n] = 0;
        c[i]++;
        break;
      }
    }
  } else {
    int xx;
    if (n == 1)
      xx = 1;
    else
      xx = 0;
    for (i = b[n]; i >= xx; i--) {
      if (i == b[n] && c[i] != 0) {
        c[i]--;
        s[n] = i;
        dp(n + 1, s, ss * 10 + i, c, 0);
        s[n] = 0;
        c[i]++;
      } else if (c[i] != 0) {
        c[i]--;
        s[n] = i;
        dp(n + 1, s, ss * 10 + i, c, 1);
        s[n] = 0;
        c[i]++;
        break;
      }
    }
  }
  return;
}
int main() {
  getline(cin, st);
  getline(cin, sss);
  int i;
  for (i = 0; (unsigned int)i < st.size(); i++) {
    a[st[i] - '0']++;
    aa[i + 1] = st[i] - '0';
  }
  for (i = 0; (unsigned int)i < sss.size(); i++) {
    b[i + 1] = sss[i] - '0';
  }
  if (st.size() < sss.size()) {
    sort(aa + 1, aa + st.size() + 1, cmp);
    for (i = 1; (unsigned int)i <= st.size(); i++) {
      printf("%d", aa[i]);
    }
    return 0;
  }
  dp(1, d, 0, a, 0);
  cout << ans;
  return 0;
}
