#include <bits/stdc++.h>
using namespace std;
const long long int mod = 1e9 + 7;
int phi(int n) {
  int result = n;
  for (int i = 2; i * i <= n; i++)
    if (n % i == 0) {
      while (n % i == 0) n = n / i;
      result -= result / i;
    }
  if (n > 1) result -= result / n;
  return result;
}
long long int mod_pow(long long int base, long long int e) {
  long long int ans = 1;
  while (e) {
    if (e & 1) ans = (ans * base) % mod;
    base = (base * base) % mod;
    e >>= 1;
  }
  return ans;
}
int gcd(int a, int b) { return b ? gcd(b, a % b) : a; }
string a, b, ans;
void solve(int arr[], int j) {
  if (ans.length() == a.length()) {
    return;
  }
  bool pre = false;
  if (arr[b[j] - '0'] > 0) {
    arr[b[j] - '0']--;
    ans += b[j];
    solve(arr, j + 1);
    if (ans.length() == a.length()) {
      return;
    }
    arr[b[j] - '0']++;
    ans = ans.substr(0, ans.length() - 1);
  }
  for (int i = b[j] - '0' - 1; i >= 0; i--) {
    if (arr[i] > 0) {
      arr[i]--;
      pre = true;
      ans += char(i + '0');
      break;
    }
  }
  if (pre) {
    for (int i = 9; i >= 0; i--) {
      while (arr[i]) {
        ans += char(i + '0');
        arr[i]--;
      }
    }
  }
  return;
}
int main() {
  cin >> a >> b;
  if (b.length() > a.length()) {
    sort(a.begin(), a.end());
    for (int i = a.length() - 1; i >= 0; i--) cout << a[i];
    return 0;
  }
  int arr[10] = {0};
  for (int i = 0; i < a.length(); i++) arr[a[i] - '0']++;
  solve(arr, 0);
  cout << ans << "\n";
}
