#include <bits/stdc++.h>
using namespace std;
const int MAXN = 50020;
int v[MAXN];
string s, b;
int tab[10];
long long target;
void completa(long long *n) {
  long long &x = *n;
  while (log10(x) + 1 < b.size()) {
    x *= 10;
  }
}
long long faz_num(string s) {
  int tam = s.size();
  long long k = 1;
  long long num = 0;
  for (int i = tam - 1; i >= 0; i--, k *= 10) num += (s[i] - '0') * k;
  if (log10(num) + 1 < b.size() && num) completa(&num);
  return num;
}
bool f = 0;
void vai(string a, int used) {
  if (faz_num(a) > target) return;
  if (used == (int)b.size()) {
    f = 1;
    cout << a << '\n';
    return;
  }
  int i = 9;
  if (!f) {
    while (i >= 0) {
      if (tab[i]) {
        tab[i]--;
        a += (i + 48);
        vai(a, used + 1);
        tab[i]++;
        a.pop_back();
      }
      i--;
    }
  }
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  memset(tab, 0, sizeof tab);
  cin >> b >> s;
  target = faz_num(s);
  for (size_t i = 0; i < b.size(); i++) {
    tab[b[i] - '0']++;
  }
  f = 0;
  string aux;
  vai(aux, 0);
}
