#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int a[300005], b[300005];
int la, lb;
int cnt[10];
void printCnt() {
  for (int j = 9; j >= 0; j--) {
    for (int i = 1; i <= cnt[j]; i++) cout << char('0' + j);
  }
}
int top = 0;
bool chk(int id, int k) {
  if (k < b[id]) return 1;
  top = 0;
  for (int j = 0; j < 10; j++) {
    for (int i = 1; i <= cnt[j]; i++) {
      ++top;
      if (j > b[id + top]) return 0;
      if (j < b[id + top]) return 1;
    }
  }
  return 1;
}
int main() {
  cin >> s1 >> s2;
  la = s1.length();
  lb = s2.length();
  for (int i = 0; i < la; i++) a[i] = s1[i] - '0';
  for (int i = 0; i < lb; i++) b[i] = s2[i] - '0';
  if (la < lb) {
    sort(a, a + la);
    for (int i = la - 1; i >= 0; i--) cout << a[i];
    return 0;
  }
  for (int i = 0; i < la; i++) ++cnt[a[i]];
  for (int i = 0; i < la; i++) {
    for (int j = 9; j >= 0; j--) {
      if (cnt[j] && j <= b[i]) {
        --cnt[j];
        if (chk(i, j)) {
          cout << char(j + '0');
          if (j < b[i]) {
            printCnt();
            return 0;
          }
          break;
        }
        ++cnt[j];
      }
    }
  }
  return 0;
}
