#include <bits/stdc++.h>
using namespace std;
int vis[10];
int bn[20];
int ans[20];
int dfs(int pos, bool limit) {
  if (pos == -1) {
    return 1;
  }
  int up;
  if (limit)
    up = bn[pos];
  else
    up = 9;
  for (int i = up; i >= 0; --i) {
    if (vis[i] > 0) {
      vis[i]--;
      if (dfs(pos - 1, limit && i == bn[pos])) {
        ans[pos] = i;
        return 1;
      }
      vis[i]++;
    }
  }
  return 0;
}
int main() {
  long long a, b;
  while (cin >> a >> b) {
    long long tmp;
    tmp = a;
    int la, lb;
    la = lb = 0;
    while (tmp) {
      vis[tmp % 10]++;
      tmp /= 10;
      la++;
    }
    tmp = b;
    while (tmp) {
      bn[lb++] = tmp % 10;
      tmp /= 10;
    }
    dfs(la - 1, la == lb);
    for (int i = la - 1; i >= 0; --i) {
      printf("%d", ans[i]);
    }
  }
  return 0;
}
