#include <bits/stdc++.h>
using namespace std;
const int N = 20;
string s1, s2;
int len1, len2;
int bo[N], a[N], b[N];
vector<int> v, vans;
bool get_ans() {
  for (int i = 0; i < (int)vans.size(); i++) v[i + 1] = vans[i];
  int nn = (int)vans.size();
  for (int i = 0; i <= 9; i++)
    for (int j = 1; j <= bo[i]; j++) v[++nn] = i;
  for (int i = 1; i <= len1; i++)
    if (v[i] != b[i] && v[i] > b[i])
      return false;
    else if (v[i] != b[i] && v[i] < b[i])
      return true;
  return true;
}
int main() {
  ios::sync_with_stdio(0), cin.tie(0);
  cin >> s1 >> s2;
  len1 = s1.size(), len2 = s2.size();
  if (len1 < len2) {
    sort(s1.begin(), s1.end());
    reverse(s1.begin(), s1.end());
    cout << s1 << endl;
    return 0;
  }
  v.resize(len1 + 1);
  for (int i = 0; i < len1; i++) a[i + 1] = s1[i] - '0';
  for (int i = 0; i < len2; i++) b[i + 1] = s2[i] - '0';
  for (int i = 1; i <= len1; i++) bo[a[i]]++;
  bool haved = 0;
  for (int i = 0; i < len1; i++) {
    for (int j = 9; j >= 0; j--)
      if (bo[j]) {
        int k = s2[i] - '0';
        if (j <= k || haved) {
          bo[j]--;
          vans.push_back(j);
          if (get_ans()) {
            if (j < k) haved = 1;
            break;
          }
          vans.pop_back();
          bo[j]++;
        }
      }
  }
  for (int i = 0; i < (int)vans.size(); i++) cout << vans[i];
  return 0;
}
