#include <bits/stdc++.h>
using namespace std;
long long n, m, i, j, q, o, ot;
vector<long long> a, b;
long long f[33];
void rc(long long s, long long j) {
  if (j == n) {
    for (long long j = 0; j < n; j++)
      if (f[j] != 1) s = s * 10 + a[j];
    ot = max(ot, s);
    return;
  }
  for (long long i = 0; i < n; i++)
    if (f[i] != 1)
      if (a[i] < b[j]) {
        f[i] = 1;
        long long ss = s;
        ss = ss * 10 + a[i];
        for (long long j = 0; j < n; j++)
          if (f[j] != 1) ss = ss * 10 + a[j];
        f[i] = 0;
        ot = max(ss, ot);
        break;
      }
  for (long long i = 0; i < n; i++)
    if (f[i] != 1)
      if (a[i] == b[j]) {
        f[i] = 1;
        rc(s * 10 + a[i], j + 1);
        f[i] = 0;
        break;
      }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin >> n;
  cin >> m;
  while (n != 0) {
    a.push_back(n % 10);
    n /= 10;
  }
  while (m != 0) {
    b.push_back(m % 10);
    m /= 10;
  }
  if (b.size() > a.size()) {
    sort(a.begin(), a.end());
    reverse(a.begin(), a.end());
    for (i = 0; i < a.size(); i++) cout << a[i];
    return 0;
  }
  sort(a.begin(), a.end());
  reverse(a.begin(), a.end());
  reverse(b.begin(), b.end());
  m = 1 << a.size();
  m--;
  q = a.size();
  n = a.size();
  for (i = 0; i < n; i++)
    if (a[i] < b[0]) {
      f[i] = 1;
      ot = a[i];
      for (j = 0; j < n; j++)
        if (f[j] == 0) ot = ot * 10 + a[j];
      f[i] = 0;
      break;
    }
  for (i = 0; i < n; i++)
    if (a[i] == b[0]) {
      f[i] = 1;
      rc(a[i], 1);
      f[i] = 0;
      break;
    }
  cout << ot << endl;
}
