#include <bits/stdc++.h>
using namespace std;
int arr[101];
map<char, int> s1map;
map<char, int> s2map;
vector<int> answer;
string s1;
string s2;
int main() {
  cin >> s1;
  for (int i = 0; i < s1.length(); i++) s1map[s1[i]]++;
  cin >> s2;
  int sz1 = s1.length();
  int sz2 = s2.length();
  if (sz1 < sz2) {
    vector<int> temp;
    for (int i = 0; i < s1.length(); i++) temp.push_back(s1[i] - '0');
    sort(temp.begin(), temp.end());
    for (int i = temp.size() - 1; i >= 0; i--) cout << temp[i];
    cout << endl;
    return 0;
  }
  for (int i = 0; i < s1.length(); i++) {
    map<char, int> copymap;
    vector<int> temp;
    for (int c = '0'; c <= '9'; c++) copymap[c] = s1map[c];
    for (int i = 0; i < answer.size(); i++) temp.push_back(answer[i]);
    for (int j = '9'; j >= '0'; j--) {
      if (copymap[j] > 0) {
        copymap[j]--;
        temp.push_back(j - '0');
        bool possible = true;
        for (int k = '0'; k <= '9'; k++) {
          while (copymap[k] > 0) {
            temp.push_back(k - '0');
            copymap[k]--;
          }
        }
        for (int k = 0; k < temp.size(); k++) {
          if (temp[k] < s2[k] - '0') {
            possible = true;
            break;
          } else if (temp[k] > s2[k] - '0') {
            possible = false;
            break;
          }
        }
        if (possible) {
          s1map[j]--;
          answer.push_back(j - '0');
          break;
        } else {
          for (int c2 = '0'; c2 <= '9'; c2++) copymap[c2] = s1map[c2];
          temp.clear();
          for (int i = 0; i < answer.size(); i++) temp.push_back(answer[i]);
        }
      }
    }
  }
  for (int i = 0; i < answer.size(); i++) cout << answer[i];
}
