#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int main() {
  cin >> s1 >> s2;
  long long int i, j, k, n;
  if (s2.size() > s1.size()) {
    sort(s1.begin(), s1.end());
    reverse(s1.begin(), s1.end());
    cout << s1 << "\n";
    return 0;
  }
  bool flag = false;
  if (s1 == s2) {
    cout << s1;
    return 0;
  }
  n = s2.size();
  for (j = n; j >= 0; j--) {
    int hsh[15] = {0};
    for (i = 0; i < n; i++) hsh[s1[i] - '0']++;
    long long int cnt = 0;
    string ans = "";
    for (i = 0; i < j; i++) {
      if (hsh[s2[i] - '0'] > 0) {
        cnt++;
        hsh[s2[i] - '0']--;
        char x = s2[i];
        ans += x;
      }
    }
    if (cnt != j) continue;
    if (cnt == n) {
      cout << ans << "\n";
      return 0;
    }
    char y = s2[j];
    y--;
    for (char x = y; x >= '0'; x--) {
      if (hsh[x - '0'] > 0) {
        ans += x;
        hsh[x - '0']--;
        break;
      }
    }
    for (char x = '9'; x >= '0'; x--) {
      int cnt = hsh[x - '0'];
      while (cnt > 0) {
        ans += x;
        cnt--;
      }
    }
    if (ans <= s2) {
      cout << ans;
      return 0;
    }
  }
}
