#include <bits/stdc++.h>
using namespace std;
int K;
string a, b;
char s[20];
int fr[10];
void rec(int p, int ok) {
  if (p >= a.size()) {
    cout << s;
    K = 1;
    return;
  }
  if (ok == 1) {
    for (int j = 9; j >= 0; j--)
      if (fr[j]) {
        s[p] = (char)(j + '0');
        fr[j]--;
        rec(p + 1, ok);
      }
    return;
  }
  for (int j = 9; j >= 0; j--)
    if (fr[j] && j <= b[p] - '0') {
      if (j <= b[p] - '0') {
        s[p] = (char)(j + '0');
        fr[j]--;
      }
      if (j < b[p] - '0')
        rec(p + 1, 1);
      else
        rec(p + 1, ok);
      if (K == 1) return;
      fr[j]++;
    }
}
int main() {
  cin >> a >> b;
  int ok = 0;
  for (int i = 0; i < a.size(); i++) fr[a[i] - '0']++;
  if (b.size() > a.size()) ok = 1;
  rec(0, ok);
  return 0;
}
