#include <bits/stdc++.h>
using namespace std;
long long compute(const string& str) {
  long long res = 0;
  for (char c : str) {
    res = res * 10 + (c - '0');
  }
  return res;
}
string minimal_suffix(const vector<int>& digit_count) {
  string res;
  for (int d = 0; d <= 9; ++d) {
    for (int i = 0; i < digit_count[d]; ++i) {
      res += '0' + d;
    }
  }
  return res;
}
int main() {
  string a, b;
  cin >> a >> b;
  if (b.size() > a.size()) {
    sort(a.rbegin(), a.rend());
    cout << a;
    return 0;
  }
  vector<int> digit_count(10, 0);
  for (char c : a) {
    digit_count[c - '0']++;
  }
  string max_res;
  for (int pos = 0; pos < a.size(); ++pos) {
    for (int d = 9; d >= 0; --d) {
      if (digit_count[d] == 0) continue;
      string cur = max_res;
      cur += '0' + d;
      digit_count[d]--;
      cur += minimal_suffix(digit_count);
      if (compute(cur) <= compute(b)) {
        max_res = cur.substr(0, pos + 1);
        break;
      } else {
        digit_count[d]++;
      }
    }
  }
  cout << max_res;
  return 0;
}
