#include <bits/stdc++.h>
using namespace std;
using vi = vector<long long int>;
using vvi = vector<vi>;
using vb = vector<bool>;
using vc = vector<char>;
using vs = vector<string>;
using vld = vector<long double>;
using pii = pair<long long int, long long int>;
using psi = pair<string, long long int>;
using pci = pair<char, long long int>;
using vpii = vector<pii>;
long long int mod = 1e9 + 7;
long long int const maxn = 1e5 + 5;
long long int const inf = 1e18;
long long int dx[] = {0, 0, 1, -1};
long long int dy[] = {1, -1, 0, 0};
long long int add(long long int a, long long int b) {
  return ((a % mod) + (b % mod)) % mod;
}
long long int mul(long long int a, long long int b) {
  return ((a % mod) * (b % mod)) % mod;
}
long long int powm(long long int x, long long int n, long long int M) {
  long long int result = 1;
  while (n > 0) {
    if (n % 2 == 1) result = (result * x) % M;
    x = (x * x) % M;
    n = n / 2;
  }
  return result;
}
long long int modinverse(long long int a, long long int m) {
  return powm(a, m - 2, m);
}
bool prime(long long int x) {
  if (x <= 1) return false;
  for (int i = 2; i <= sqrt(x); i++)
    if (x % i == 0) return false;
  return true;
}
long long int divisor(long long int x) {
  long long int cnt = 0;
  for (int i = 1; i <= sqrt(x); i++) {
    if (x % i == 0) {
      if (i != x / i)
        cnt += 2;
      else
        cnt += 1;
    }
  }
  return cnt;
}
vector<long long int> sieve(long long int n) {
  bool prim[n + 1];
  memset(prim, true, sizeof(prim));
  for (long long int p = 2; p * p <= n; p++) {
    if (prim[p] == true) {
      for (int i = p * p; i <= n; i += p) prim[i] = false;
    }
  }
  vector<long long int> v;
  for (int i = 2; i <= n; i++)
    if (prim[i]) v.push_back(i);
  return v;
}
void solve() {
  string a, b;
  cin >> a >> b;
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  if ((long long int)a.size() < (long long int)b.size()) {
    cout << a;
    return;
  }
  vi freq(10, 0);
  for (auto i : a) freq[i - '0']++;
  for (auto i : b) {
    freq[i - '0']--;
  }
  bool f = true;
  for (int i = 0; i <= 9; i++) {
    if (freq[i] < 0) f = false;
  }
  if (f) {
    cout << b;
    return;
  }
  long long int n = (long long int)b.size();
  for (int i = n - 1; i >= 0; i--) {
    vi freq(10, 0);
    for (auto i : a) freq[i - '0']++;
    string x = "";
    bool f = true;
    for (int j = 0; j < i; j++) {
      if (freq[b[j] - '0'] == 0) {
        f = false;
        break;
      }
      x += b[j];
      freq[b[j] - '0']--;
    }
    for (auto j : x) {
      if (j == '0') {
        f = false;
        break;
      } else
        break;
    }
    if (!f) continue;
    f = false;
    for (int j = (b[i] - '0') - 1; j >= 0; j--) {
      if (freq[j]) {
        f = true;
        x += (j + '0');
        freq[j]--;
        break;
      }
    }
    if (!f) continue;
    string p = "";
    for (int j = 0; j <= 9; j++) {
      for (int k = 1; k <= freq[j]; k++) p += (j + '0');
    }
    sort(p.begin(), p.end());
    reverse(p.begin(), p.end());
    long long int rem_l = (long long int)b.size() - (long long int)x.size();
    for (int j = 0; j < rem_l; j++) x += p[j];
    cout << x << "\n";
    return;
  }
}
int main() {
  long long int t;
  t = 1;
  while (t--) {
    solve();
  }
  return 0;
}
