#include <bits/stdc++.h>
using namespace std;
int ad[100] = {0};
int bd[100] = {0};
int lena = 0;
int lenb = 0;
int retVal[100] = {0};
int retIdx = 0;
void bar(int idx) {
  int tmp[100] = {0};
  int len = 0;
  for (int i = 0; i < lenb; i++) {
    if (bd[i] != -1) {
      tmp[len++] = bd[i];
    }
  }
  sort(tmp, tmp + len, greater<int>());
  for (int i = 0; i < len; i++) {
    retVal[retIdx++] = tmp[i];
  }
  return;
}
int foo(int idx) {
  int minDig = -1;
  int minDigIdx = -1;
  int minDig2 = -1;
  int minDigIdx2 = -1;
  bool doneb = false;
  for (int i = 0; i < lenb; i++) {
    if (bd[i] != -1) {
      doneb = true;
    }
    if (bd[i] <= ad[idx] && (bd[i] > minDig)) {
      minDig = bd[i];
      minDigIdx = i;
    }
    if (bd[i] < ad[idx] && (bd[i] > minDig2)) {
      minDig2 = bd[i];
      minDigIdx2 = i;
    }
  }
  if (!doneb) {
    return 0;
  }
  if (doneb && minDig == -1) {
    return -1;
  }
  if (minDig == ad[idx]) {
    bd[minDigIdx] = -1;
    retVal[retIdx++] = minDig;
    int ret = foo(idx + 1);
    if (ret == -1) {
      bd[minDigIdx] = minDig;
      retIdx--;
      retVal[retIdx] = 0;
      if (minDig2 == -1) {
        return -1;
      }
      bd[minDigIdx2] = -1;
      retVal[retIdx++] = minDig2;
      bar(idx + 1);
    }
  } else if (minDig < ad[idx]) {
    bd[minDigIdx] = -1;
    retVal[retIdx++] = minDig;
    bar(idx + 1);
  }
  return 0;
}
void revad(int start, int end) {
  int temp;
  while (start < end) {
    temp = ad[start];
    ad[start] = ad[end];
    ad[end] = temp;
    start++;
    end--;
  }
}
void revbd(int start, int end) {
  int temp;
  while (start < end) {
    temp = bd[start];
    bd[start] = bd[end];
    bd[end] = temp;
    start++;
    end--;
  }
}
int main() {
  long long a, b;
  scanf("%lld", &b);
  scanf("%lld", &a);
  while (a != 0) {
    ad[lena++] = a % 10;
    a /= 10;
  }
  while (b != 0) {
    bd[lenb++] = b % 10;
    b /= 10;
  }
  revad(0, lena - 1);
  revbd(0, lenb - 1);
  if (lenb < lena) {
    bar(0);
  } else {
    foo(0);
  }
  for (int i = 0; i < retIdx; i++) {
    printf("%d", retVal[i]);
  }
  printf("\n");
  return 0;
}
