#include <bits/stdc++.h>
using namespace std;
const long long N = 3e5 + 5;
vector<long long> f;
vector<long long> s;
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  ;
  long long a, b;
  cin >> a >> b;
  while (a > 0) {
    long long rem = a % 10;
    a /= 10;
    f.push_back(rem);
  }
  while (b > 0) {
    long long rem = b % 10;
    b /= 10;
    s.push_back(rem);
  }
  reverse(f.begin(), f.end());
  reverse(s.begin(), s.end());
  long long dp[10];
  memset(dp, 0, sizeof(dp));
  for (auto x : f) {
    dp[x]++;
  }
  if (f.size() < s.size()) {
    sort(f.begin(), f.end(), greater<long long>());
    for (auto x : f) {
      cout << x;
    }
    cout << endl;
    return 0;
  }
  bool done = 0;
  for (long long i = 0; i < s.size(); ++i) {
    long long x = s[i];
    for (long long num = x; num >= 0; --num) {
      if (dp[num] > 0) {
        if (num == x) {
          dp[num]--;
          vector<long long> rem;
          for (long long j = 0; j < 10; ++j) {
            long long tmp = dp[j];
            while (tmp--) {
              rem.push_back(j);
            }
          }
          sort(rem.begin(), rem.end());
          bool val = 1;
          for (long long j = i + 1; j < s.size(); ++j) {
            if (rem[j - (i + 1)] > s[j]) val = 0;
            if (rem[j - (i + 1)] < s[j]) break;
          }
          if (!val) {
            dp[num]++;
            continue;
          }
          dp[num]++;
        }
        cout << num;
        if (num < x) done = 1;
        dp[num]--;
        break;
      }
    }
    if (done) break;
  }
  vector<long long> rem;
  for (long long i = 0; i < 10; ++i) {
    while (dp[i] > 0) {
      rem.push_back(i);
      dp[i]--;
    }
  }
  sort(rem.begin(), rem.end(), greater<long long>());
  for (auto x : rem) {
    cout << x;
  }
  cout << endl;
}
