#include <bits/stdc++.h>
using namespace std;
template <typename T>
void chmax(T& a1, T a2) {
  a1 = std::max(a1, a2);
}
template <typename T>
void chmin(T& a1, T a2) {
  a1 = std::min(a1, a2);
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  return os << "(" << p.first << ", " << p.second << ")";
}
template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  os << "[";
  for (int i = 0; i < v.size(); i++) os << (i == 0 ? "" : ", ") << v[i];
  os << "]";
  return os;
}
template <typename T1, typename T2>
ostream& operator<<(ostream& os, map<T1, T2>& mp) {
  os << "{";
  for (auto it = mp.begin(); it != mp.end(); it++) {
    os << "(" << it->first << ": " << it->second << ")";
    it++;
    if (it != mp.end()) os << ", ";
    it--;
  }
  os << "}";
  return os;
}
template <typename T>
ostream& operator<<(ostream& os, set<T>& st) {
  os << "{";
  for (auto it = st.begin(); it != st.end(); it++) {
    os << *it;
    ++it;
    if (it != st.end()) os << ", ";
    it--;
  }
  os << "}";
  return os;
}
using int64 = long long;
using Graph = std::vector<std::vector<int>>;
int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  string A, B;
  cin >> A >> B;
  if (A.size() < B.size()) {
    sort(A.begin(), A.end(), greater<char>());
    cout << A << endl;
    return 0;
  }
  vector<int> cnt(10, 0);
  for (char ch : A) cnt[ch - '0']++;
  string ans = "";
  for (long long i = 0; (i) < (long long)(A.size() + 1); (i)++) {
    vector<int> cnt_copy = cnt;
    bool flag = true;
    for (long long j = 0; (j) < (long long)(i); (j)++) {
      int d = B[j] - '0';
      if (cnt_copy[d] <= 0) flag = false;
      cnt_copy[d]--;
    }
    if (not flag) continue;
    if (i == A.size()) {
      ans = B;
      continue;
    }
    int digit = -1;
    for (long long d = 0; (d) < (long long)(B[i] - '0'); (d)++) {
      if (cnt_copy[d] > 0) digit = d;
    }
    if (digit < 0) continue;
    string res = B.substr(0, i);
    res.push_back(digit + '0');
    cnt_copy[digit]--;
    for (long long j = (long long)(i + 1); (j) < (long long)(A.size()); (j)++) {
      for (int d = 9; d >= 0; d--) {
        if (cnt_copy[d] > 0) {
          res.push_back(d + '0');
          cnt_copy[d]--;
          break;
        }
      }
    }
    ans = max(ans, res);
  }
  cout << ans << endl;
  return 0;
}
