#include <bits/stdc++.h>
using namespace std;
const long long N = 102;
string q1, q2;
long long cnt1[10] = {0};
string ans;
void dp(long long a1, long long flag) {
  if (a1 == q1.size()) {
    cout << ans << endl;
    exit(0);
  }
  long long lim = 9;
  if (!flag) lim = q2[a1] - '0';
  for (long long i = lim; i >= 0; i--) {
    if (cnt1[i]) {
      ans += char(i + '0');
      cnt1[i]--;
      long long u1 = 1;
      if (flag == 0 && (i == lim)) u1 = 0;
      dp(a1 + 1, u1);
      ans.pop_back();
      cnt1[i]++;
    }
  }
}
long long binpow(long long a, long long b, long long m) {
  a %= m;
  long long res = 1;
  while (b > 0) {
    if (b & 1) res = res * a % m;
    a = a * a % m;
    b >>= 1;
  }
  return res;
}
long long inv(long long a) {
  a %= 1000000007;
  if (a < 0) a += 1000000007;
  long long b = 1000000007, u = 0, v = 1;
  while (a) {
    long long t = b / a;
    b -= t * a;
    swap(a, b);
    u -= t * v;
    swap(u, v);
  }
  if (u < 0) u += 1000000007;
  return u;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> q1 >> q2;
  if (q1.size() < q2.size()) {
    sort(q1.begin(), q1.end());
    reverse(q1.begin(), q1.end());
    cout << q1 << endl;
  } else {
    for (long long i = 0; i < q1.size(); i++) cnt1[q1[i] - '0']++;
    dp(0, 0);
  }
}
