#include <bits/stdc++.h>
std::string memo[2][20];
static std::string go(const std::string& bound, const std::string& source,
                      std::vector<char>& used, const long long& k,
                      const bool& eq, std::string& curr) {
  if (k >= bound.length()) return curr;
  if (memo[eq][k] != "") return memo[eq][k];
  char top = eq ? bound[k] : '9';
  std::string ret = "0";
  for (long long i = 0; i < source.length(); ++i) {
    if (used[i] || source[i] > top) continue;
    used[i] = true;
    curr[k] = source[i];
    ret = std::max(ret,
                   go(bound, source, used, k + 1, eq && curr[k] == top, curr));
    used[i] = false;
  }
  return memo[eq][k] = ret;
}
int main(void) {
  std::string a, b;
  std::cin >> a >> b;
  std::sort(a.begin(), a.end(),
            [](const char& a, const char& b) { return a > b; });
  if (a.length() < b.length()) {
    std::cout << a << "\n";
    return 0;
  }
  for (long long i = 0; i < 2; ++i) std::fill(memo[i], memo[i] + 20, "");
  std::vector<char> used(a.length(), false);
  std::string curr = a;
  std::printf("%s\n", go(b, a, used, 0, true, curr).data());
  return 0;
}
