#include <bits/stdc++.h>
const int64_t kInfty = 1e18;
const double kEps = 1e-7;
int64_t FindDigits(int64_t a) {
  int64_t res = 0;
  while (a > 0) {
    ++res;
    a /= 10;
  }
  return res;
}
std::vector<int64_t> Extract(int64_t a) {
  std::vector<int64_t> res;
  while (a > 0) {
    res.push_back(a % 10);
    a /= 10;
  }
  std::reverse(res.begin(), res.end());
  return res;
}
int32_t main() {
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(nullptr);
  int64_t a, b;
  std::cin >> a >> b;
  int64_t dig_a = FindDigits(a);
  int64_t dig_b = FindDigits(b);
  std::vector<int64_t> dig = Extract(a);
  if (dig_a < dig_b) {
    std::sort(dig.begin(), dig.end(), std::greater<int64_t>());
    for (int64_t digit : dig) std::cout << digit;
    return 0;
  }
  std::sort(dig.begin(), dig.end());
  std::vector<int64_t> target = Extract(b);
  std::vector<int64_t> res;
  for (size_t i = 0; i < target.size(); ++i) {
    int64_t insert = target[i];
    for (; insert >= 0; --insert) {
      bool ok = false;
      for (auto it = dig.begin(); it != dig.end(); ++it)
        if (*it == insert) {
          ok = true;
          res.push_back(insert);
          dig.erase(it);
          break;
        }
      if (ok) break;
    }
    if (insert < 0) {
      while (!res.empty()) {
        int64_t del = *res.rbegin();
        dig.push_back(del);
        res.pop_back();
        int64_t new_insert = del - 1;
        for (; new_insert >= 0; --new_insert) {
          bool ok = false;
          for (auto it = dig.begin(); it != dig.end(); ++it)
            if (*it == new_insert) {
              res.push_back(new_insert);
              ok = true;
              dig.erase(it);
              break;
            }
          if (ok) break;
        }
        if (new_insert < 0)
          continue;
        else {
          std::sort(dig.begin(), dig.end(), std::greater<int64_t>());
          for (int64_t x : res) std::cout << x;
          for (int64_t x : dig) std::cout << x;
          return 0;
        }
      }
    }
    if (insert < target[i]) {
      for (int64_t x : res) std::cout << x;
      for (auto it = dig.rbegin(); it != dig.rend(); ++it) std::cout << *it;
      return 0;
    }
  }
  for (int64_t val : res) std::cout << val;
}
