#include <bits/stdc++.h>
using namespace std;
inline long long bip(long long x, long long n, long long mod) {
  long long res = 1;
  while (n) {
    if (n & 1) {
      res = (res * x) % mod;
    }
    x = (x * x) % mod;
    n >>= 1;
  }
  return res;
}
const int maxn = INT_MAX;
const long long inf = 1e18;
const int mod = 998244353;
const int N = 1e5 + 10;
void solve() {
  string a, b;
  cin >> a >> b;
  if ((int)b.size() > (int)a.size()) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    cout << a;
    return;
  }
  vector<int> cnt(10, 0);
  for (int i = 0; i < (int)a.size(); i++) cnt[a[i] - '0']++;
  vector<int> ans;
  for (int i = 0; i < (int)b.size(); i++) {
    int k = b[i] - '0';
    bool ok = 0;
    for (int j = k; j >= 0; j--) {
      if (cnt[j]) {
        ok = 1;
        cnt[j]--;
        ans.push_back(j);
        if (j < k) {
          for (int l = 9; l >= 0; l--) {
            while (cnt[l]--) ans.push_back(l);
          }
          for (auto it : ans) cout << it;
          return;
        }
        break;
      }
    }
    if (ok == 0) {
      for (int p = i - 1; p >= 0; p--) {
        int k = b[p] - '0';
        ans.pop_back();
        cnt[k]++;
        bool ok = 0;
        for (int j = k - 1; j >= 0; j--) {
          if (cnt[j]) {
            ok = 1;
            break;
          }
        }
        if (ok) {
          for (int j = k - 1; j >= 0; j--) {
            if (cnt[j]) {
              ans.push_back(j);
              cnt[j]--;
              for (int l = 9; l >= 0; l--) {
                while (cnt[l]--) ans.push_back(l);
              }
              for (auto it : ans) cout << it;
              return;
            }
          }
        }
      }
    }
  }
  for (auto it : ans) cout << it;
}
int main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  int tt = 1;
  while (tt--) {
    solve();
    cout << '\n';
  }
}
