#include <bits/stdc++.h>
using namespace std;
int main() {
  string a, b;
  vector<int> vb, ans, ans2, tmp;
  vector<pair<int, int> > va;
  ans.clear();
  ans2.clear();
  tmp.clear();
  int i, j, cnt = 0, x, mx, mxpos, fl;
  cin >> a >> b;
  for (i = 0; i < a.size(); i++) {
    va.push_back(make_pair(a[i] - '0', 0));
    tmp.push_back(0);
  }
  for (i = 0; i < b.size(); i++) vb.push_back(b[i] - '0');
  sort(va.begin(), va.end());
  if (a.size() == b.size()) {
    for (i = 0; i < a.size(); i++) {
      mxpos = -1;
      x = vb[i];
      mx = -1;
      fl = 0;
      ans2 = ans;
      for (j = 0; j < a.size(); j++) {
        if (va[j].first < x && va[j].second == 0) {
          if (va[j].first > mx) {
            mx = va[j].first;
            mxpos = j;
          }
        }
      }
      if (mxpos != -1 && (mx != 0 || ans2.size() != 0)) {
        ans2.push_back(mx);
        for (j = a.size() - 1; j >= 0; j--)
          if (va[j].second == 0 && j != mxpos) ans2.push_back(va[j].first);
        for (j = 0; j < a.size(); j++) {
          if (ans2[j] > tmp[j]) {
            tmp = ans2;
            break;
          } else if (ans2[j] < tmp[j])
            break;
        }
      }
      for (j = 0; j < a.size(); j++) {
        if (va[j].first == x && va[j].second == 0) {
          va[j].second = 1;
          fl = 1;
          ans.push_back(va[j].first);
          break;
        }
      }
      if (fl == 0) break;
    }
  }
  if (a.size() == b.size()) {
    if (ans.size() == a.size()) tmp = ans;
    for (i = 0; i < tmp.size(); i++) cout << tmp[i];
  } else {
    for (i = va.size() - 1; i >= 0; i--) cout << va[i].first;
  }
  cout << endl;
  return 0;
}
