#include <bits/stdc++.h>
using namespace std;
int n, m;
char a[32], b[32];
int dig[10];
long long check(int to) {
  long long ans = 0;
  int dig2[10];
  for (int i = 0; i < 10; ++i) {
    dig2[i] = dig[i];
  }
  for (int i = 0; i < to; ++i) {
    if (!dig2[b[i] - '0']) {
      return -1;
    }
    ans *= 10;
    ans += b[i] - '0';
    --dig2[b[i] - '0'];
  }
  if (to == m) {
    return ans;
  }
  bool flag = 0;
  for (int i = b[to] - '0' - 1; i >= 0; --i) {
    if (dig2[i]) {
      --dig2[i];
      ans *= 10;
      ans += i;
      flag = 1;
      break;
    }
  }
  if (!flag) {
    return -1;
  }
  for (int i = 9; i >= 0; --i) {
    while (dig2[i]) {
      ans *= 10;
      ans += i;
      --dig2[i];
    }
  }
  return ans;
}
int main() {
  cin >> a >> b;
  n = strlen(a);
  m = strlen(b);
  for (int i = 0; i < n; ++i) {
    ++dig[a[i] - '0'];
  }
  if (n < m) {
    for (int i = 9; i >= 0; --i) {
      while (dig[i]) {
        cout << i;
        --dig[i];
      }
    }
    cout << "\n";
    return 0;
  }
  for (int i = m; i >= 0; --i) {
    long long a = check(i);
    if (a != -1) {
      cout << a << "\n";
      return 0;
    }
  }
  return 0;
}
