#include <bits/stdc++.h>
using namespace std;
const long long maxn = 200010;
int main() {
  string s, s1;
  cin >> s >> s1;
  vector<int> v;
  for (int i = 0; i < s.size(); i++) {
    v.push_back(s[i] - '0');
  }
  sort(v.begin(), v.end());
  if (s.size() < s1.size()) {
    reverse(v.begin(), v.end());
    for (int i = 0; i < v.size(); i++) {
      cout << v[i];
    }
  } else {
    string ans = "";
    int f = 0, t = 0, rr = 0;
    for (int i = 0; i < s1.size(); i++) {
      int num = s1[i] - '0';
      if (t == 1) {
        v.push_back(ans[ans.size() - 1] - '0');
        sort(v.begin(), v.end());
        ans.erase(ans.begin() + ans.size() - 1);
        num = num - 1;
      }
      int p;
      if (f == 0) {
        int pos = upper_bound(v.begin(), v.end(), num) - v.begin();
        if (pos == NULL) {
          i -= 2;
          t = 1;
          continue;
        }
        p = v[pos - 1];
        v.erase(v.begin() + pos - 1);
        if (p < num || t == 1) {
          t = 0;
          f = 1;
        }
        ans += (p + '0');
      } else {
        rr = 1;
        cout << ans;
        reverse(v.begin(), v.end());
        for (int i = 0; i < v.size(); i++) {
          cout << v[i];
        }
        break;
      }
    }
    if (rr == 0) cout << ans;
  }
}
