#include <bits/stdc++.h>
using namespace std;
string A, B;
int freq[11];
int main() {
  cin >> A >> B;
  if (A.size() < B.size()) {
    sort(A.begin(), A.end());
    reverse(A.begin(), A.end());
    cout << A << "\n";
    return 0;
  }
  for (auto it : A) freq[it - '0']++;
  A.clear();
  for (int i = 0; i < B.size(); i++) {
    if (freq[B[i] - '0']) {
      A.push_back(B[i]);
      freq[B[i] - '0']--;
    } else {
      bool ok = false;
      for (int c = B[i] - '0' - 1; c >= 0; c--)
        if (freq[c] != 0) {
          A.push_back(c + '0');
          freq[c]--;
          ok = true;
          break;
        }
      if (ok) break;
      do {
        freq[A.back() - '0']++;
        A.pop_back();
        i--;
        for (int c = B[i] - '0' - 1; c >= 0; c--)
          if (freq[c] != 0) {
            A.push_back(c + '0');
            freq[c]--;
            ok = true;
            break;
          }
      } while (!ok);
      break;
    }
  }
  for (int c = 9; c >= 0; c--)
    while (freq[c]--) A.push_back(c + '0');
  cout << A << "\n";
  return 0;
}
