#include <bits/stdc++.h>
using namespace std;
string a, b;
long long int mp[10];
long long int x[20];
bool solve(long long int idx, bool isSmall) {
  if (idx == b.size()) {
    return true;
  }
  long long int start = b[idx] - '0';
  if (isSmall) start = 9;
  for (long long int j = start; j >= 0; j--) {
    if (mp[j] > 0) {
      mp[j]--;
      x[idx] = j;
      bool flag = false;
      if (isSmall) flag = true;
      if (j < start) flag = true;
      bool temp = solve(idx + 1, flag);
      if (temp) return true;
      mp[j]++;
    }
  }
  return false;
}
int main() {
  cin >> a >> b;
  if (b.size() > a.size()) {
    sort(a.rbegin(), a.rend());
    cout << a << endl;
    return 0;
  } else {
    for (long long int i = 0; i < a.size(); i++) {
      mp[a[i] - '0']++;
    }
    solve(0, false);
    for (long long int i = 0; i < b.size(); i++) {
      cout << x[i];
    }
    cout << endl;
  }
}
