#include <bits/stdc++.h>
using namespace std;
const int maxn = 50;
const long long inf = 1e18 + 7;
long long a, b;
int n = 0, m = 0;
int xa[maxn], xb[maxn];
long long gt[maxn], f[1 << 20];
long long calc(int u, int first, int ok) {
  if (u > n) return 0;
  if (f[first] != -1) return f[first];
  f[first] = -inf;
  if (ok) {
    f[first] = 0;
    for (int i = 1; i <= n; ++i)
      if (first & (1 << (i - 1))) f[first] += (long long)xa[i] * gt[n - u], ++u;
    return f[first];
  }
  for (int i = 1; i <= n; ++i)
    if (first & (1 << (i - 1)) && xa[i] <= xb[u]) {
      int second = first ^ (1 << (i - 1));
      long long o = (long long)xa[i] * gt[n - u];
      if (xa[i] < xb[u])
        f[first] = max(f[first], o + calc(u + 1, second, 1));
      else
        f[first] = max(f[first], o + calc(u + 1, second, 0));
    }
  return f[first];
}
void solve() {
  cin >> a >> b;
  while (a) xa[++n] = a % 10, a /= 10;
  sort(xa + 1, xa + n + 1, greater<int>());
  while (b) xb[++m] = b % 10, b /= 10;
  reverse(xb + 1, xb + m + 1);
  if (n < m) {
    for (int i = 1; i <= n; ++i) cout << xa[i];
    return;
  }
  gt[0] = 1;
  for (int i = 1; i < n; ++i) gt[i] = 10ll * gt[i - 1];
  for (int first = 0; first < (1 << n); ++first) f[first] = -1;
  cout << calc(1, (1 << n) - 1, 0);
}
int main() { solve(); }
