#include <bits/stdc++.h>
using namespace std;
string A, B;
int has[10];
string res;
void Create(int from) {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < has[i]; j++) {
      res[from++] = i + '0';
    }
  }
}
int main() {
  cin >> A >> B;
  int st = 0;
  for (int i = 0; i < A.length(); i++) has[A[i] - '0']++;
  while (A.length() < B.length()) {
    A = "0" + A;
    st++;
  }
  res = A;
  for (int i = st; i < A.length(); i++) {
    for (int j = 10 - 1; j >= 0; j--) {
      if (has[j] > 0) {
        has[j]--;
        res[i] = j + '0';
        Create(i + 1);
        if (res <= B) break;
        has[j]++;
      }
    }
  }
  int pos = 0;
  while (res[pos] == '0') pos++;
  cout << res.substr(pos) << endl;
  return 0;
}
