#include <bits/stdc++.h>
using namespace std;
void solve(string a, string b) {
  string s;
  vector<long long> freq(10, 0);
  for (long long i = 0; i < a.size(); i++) freq[a[i] - '0']++;
  long long flag = 1;
  for (long long i = 0; i < b.size(); i++) {
    long long change = 0;
    for (long long j = 9; j >= 0; j--) {
      if (freq[j] > 0 && j <= b[i] - '0' && flag) {
        change = 1;
        s += j + '0';
        freq[j]--;
        if (s[i] < b[i]) flag = 0;
        break;
      }
    }
    if (change == 0) break;
  }
  long long flagg = 1, pos;
  flag = 0;
  for (long long i = 0; i < s.size(); i++) {
    if (s[i] == b[i])
      continue;
    else if (s[i] < b[i]) {
      flag = 1;
      flagg = 0;
      break;
    }
  }
  if (flag == 1) {
    for (long long j = 9; j >= 0; j--) {
      while (freq[j] > 0) {
        s += j + '0';
        freq[j]--;
      }
    }
  }
  if (s.size() == a.size()) {
    cout << s << '\n';
    return;
  }
  if (flagg == 1) {
    long long check = 0;
    long long pos = s.size() - 1;
    for (long long j = pos; j >= 0; j--) {
      check = 0;
      for (long long i = 9; i >= 0; i--) {
        if (freq[i] > 0 && i < b[j] - '0') {
          freq[i]--;
          freq[s[j] - '0']++;
          s[j] = i + '0';
          check = 1;
          pos = j;
          break;
        }
      }
      if (check == 1)
        break;
      else
        freq[s[j] - '0']++;
    }
    string ans;
    for (long long i = 0; i <= pos; i++) ans += s[i];
    for (long long j = 9; j >= 0; j--) {
      while (freq[j] > 0) {
        ans += j + '0';
        freq[j]--;
      }
    }
    s = ans;
  }
  cout << s;
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  string a, b;
  cin >> a >> b;
  if (a.size() < b.size()) {
    sort(a.begin(), a.end(), greater<char>());
    cout << a << '\n';
  } else {
    solve(a, b);
  }
}
