#include <bits/stdc++.h>
using namespace std;
int cnt[10];
string a, b;
vector<int> cur;
void pr() {
  for (int i = 0; i < cur.size(); i++) cout << cur[i];
  cout << endl;
  exit(0);
}
void form() {
  for (int i = 9; i >= 0; i--) {
    for (int j = 1; j <= cnt[i]; j++) cur.push_back(i);
  }
}
void dg(int x) {
  if (x == a.size()) {
    pr();
    return;
  }
  int xx = b[x] - '0';
  if (cnt[xx]) {
    cur.push_back(xx);
    cnt[xx]--;
    dg(x + 1);
    cnt[xx]++;
    cur.pop_back();
  }
  for (int i = xx - 1; i >= 0; i--) {
    if (cnt[i]) {
      cur.push_back(i);
      cnt[i]--;
      form();
      pr();
      return;
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> a >> b;
  if (a.size() != b.size()) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    cout << a << endl;
    return 0;
  }
  for (int i = 0; i < b.size(); i++) cnt[a[i] - '0']++;
  dg(0);
  return 0;
}
