#include <bits/stdc++.h>
using namespace std;
const long long mod = 998244353;
const int maxn = 2e6 + 5;
int ca[10], caa[10], na, nb;
string a, b;
long long ans = 0, bs[20];
void dfs(int dep, long long res) {
  if (dep == nb) {
    ans = max(ans, res);
    return;
  }
  for (int i = (b[dep] - '0'); i >= (0); i--) {
    if (dep == 0 && i == 0) continue;
    if (!ca[i]) continue;
    if (i == b[dep] - '0') {
      ca[i]--;
      dfs(dep + 1, res + bs[nb - 1 - dep] * i);
      ca[i]++;
    } else {
      ca[i]--;
      res += bs[nb - 1 - dep] * i;
      long long base = 1;
      for (int i = (0); i <= (9); i++) caa[i] = ca[i];
      for (int i = (0); i <= (9); i++) {
        while (caa[i]) {
          res += base * i;
          base *= 10;
          caa[i]--;
        }
      }
      ans = max(ans, res);
      ca[i]++;
      return;
    }
  }
}
int main() {
  bs[0] = 1;
  for (int i = (1); i <= (19); i++) bs[i] = bs[i - 1] * 10;
  cin >> a >> b;
  na = a.size(), nb = b.size();
  for (int i = (0); i <= (na - 1); i++) ca[a[i] - '0']++;
  if (na < nb) {
    long long base = 1;
    for (int i = (0); i <= (9); i++) {
      while (ca[i]) {
        ans += base * i;
        base *= 10;
        ca[i]--;
      }
    }
    cout << ans;
  } else {
    dfs(0, 0);
    cout << ans;
  }
}
