#include <bits/stdc++.h>
using namespace std;
const long long MAXN = 2e5 + 100;
const long long MOD = 1e9 + 7;
long long ditgit[20];
long long store[20];
bool used[20];
char b[20];
bool cmp(char aa, char bb) { return aa > bb; }
long long ans = 0;
long long len;
bool dfs(long long pos, bool lim) {
  if (pos == len) {
    long long curans = 0;
    for (int i = 0; i < len; ++i) curans = curans * 10 + store[i];
    ans = max(ans, curans);
    return 1;
  }
  if (!lim) {
    for (int j = 0; j < len; ++j)
      if (!used[j]) {
        store[pos] = ditgit[j];
        used[j] = 1;
        dfs(pos + 1, 0);
        used[j] = 0;
        return 1;
      }
  }
  for (int i = 0; i < len; ++i) {
    if (used[i] || ditgit[i] > b[pos]) continue;
    used[i] = 1;
    store[pos] = ditgit[i];
    if (ditgit[i] < b[pos]) {
      dfs(pos + 1, 0);
      used[i] = 0;
      return 1;
    }
    if (!dfs(pos + 1, 1)) {
      used[i] = 0;
      for (int j = i; j < len; ++j)
        if (!used[j] && ditgit[j] < ditgit[i]) {
          used[j] = 1;
          store[pos] = ditgit[j];
          dfs(pos + 1, 0);
          return 1;
        }
      return 0;
    } else
      return 1;
  }
  return 0;
}
int main(int argc, char const *argv[]) {
  char a[20];
  cin >> a >> b;
  len = strlen(a);
  sort(a, a + len, cmp);
  for (int i = 0; i < len; ++i) ditgit[i] = (a[i] - '0');
  long long lenb = strlen(b);
  if (lenb > len) {
    for (int i = 0; i < len; ++i) cout << ditgit[i];
    puts("");
    return 0;
  }
  for (int i = 0; i < lenb; ++i) b[i] -= '0';
  dfs(0, 1);
  cout << ans << endl;
}
