#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
using namespace std;
template <typename T, typename TT>
inline ostream &operator<<(ostream &os, const pair<T, TT> &t) {
  return os << t.first << " " << t.second;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &t) {
  for (auto i : t) os << i << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const set<T> &t) {
  for (auto i : t) os << i << " ";
  return os;
}
template <typename T1, typename T2>
inline ostream &operator<<(ostream &os, const map<T1, T2> &t) {
  for (auto i : t) os << i.first << " : " << i.second << '\n';
  return os;
}
template <typename T>
inline istream &operator>>(istream &is, vector<T> &v) {
  for (T &t : v) is >> t;
  return is;
}
template <typename T1, typename T2>
inline istream &operator>>(istream &is, vector<pair<T1, T2>> &v) {
  for (pair<T1, T2> &t : v) is >> t.first >> t.second;
  return is;
}
const long long mod = 1e9 + 7;
multiset<char> s;
string ans = "";
void solve() {
  string a, b;
  cin >> a >> b;
  if (b.size() > a.size()) {
    sort(a.begin(), a.end(), greater<char>());
    cout << a << '\n';
    return;
  }
  for (auto x : a) s.insert(x);
  for (int i = 0; i < b.size() and !s.empty(); i++) {
    auto it = s.upper_bound(b[i]);
    if (it != s.begin()) {
      char c = *prev(it);
      ans += c;
      s.erase(s.find(c));
      if (c == b[i]) continue;
      while (!s.empty()) {
        ans += *prev(s.end());
        s.erase(prev(s.end()));
      }
      break;
    }
    long long j = i - 1;
    while (j >= 0) {
      s.insert(ans.back());
      ans.pop_back();
      it = s.upper_bound(b[j]);
      if (*prev(it) != b[j]) {
        j--;
        continue;
      }
      it = s.lower_bound(b[j]);
      if (it == s.begin()) {
        j--;
        continue;
      }
      it = prev(it);
      ans += *it;
      s.erase(it);
      break;
    }
    while (!s.empty()) {
      ans += *prev(s.end());
      s.erase(prev(s.end()));
    }
    break;
  }
  cout << ans << '\n';
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ;
  long long T = 1;
  while (T--) {
    solve();
  }
  return 0;
}
