#include <bits/stdc++.h>
using namespace std;
int pos[10];
int dp[1 << 20][2];
int path[1 << 20][2];
char letters[1 << 20];
int n;
string a, b;
int lsb(int x) { return x & -x; }
bool solve(int bit, int pref, int id) {
  if (id == n) return 1;
  int &ans = dp[bit][pref];
  if (ans != -1) return ans;
  ans = 0;
  for (int i = 9; i >= 0; i--) {
    if (pref && i > (b[id] - '0')) continue;
    if (!pos[i]) continue;
    int x = bit & pos[i];
    int y = x ^ pos[i];
    if (y) {
      int p = lsb(y);
      int newPref = i == (b[id] - '0');
      ans = solve(bit | p, pref & newPref, id + 1);
      if (ans) {
        path[bit][pref] = p;
        break;
      }
    }
  }
  return ans;
}
int main() {
  cin >> a >> b;
  sort((a).begin(), (a).end(), greater<char>());
  if (a.size() < b.size()) {
    cout << a << endl;
    return 0;
  }
  n = a.size();
  for (int i = 0; i < a.size(); i++) {
    pos[a[i] - '0'] |= 1 << i;
    letters[1 << i] = a[i];
  }
  memset(dp, -1, sizeof dp);
  solve(0, 1, 0);
  int bit = 0, pref = 1, id = 0;
  while (id != n) {
    int x = path[bit][pref];
    printf("%c", letters[x]);
    bit |= x;
    if (letters[x] < b[id]) pref = 0;
    id++;
  }
  cout << "\n";
  return 0;
}
