#include <bits/stdc++.h>
using namespace std;
const int N = (1 << 18) + 100;
int mem[3][N];
char a[20], b[20], aa, bb;
string ans;
int dp(int ok, int mask) {
  if (mask == 0) {
    return 1;
  }
  int &ret = mem[ok][mask];
  if (ret != -1) {
    return ret;
  }
  ret = 0;
  int ind = 0;
  for (long long i = 0; i <= aa - 1; i++) {
    ind += (!(mask & (1 << i)));
  }
  for (int i = 0; i < aa; i++) {
    if (mask & (1 << i)) {
      if (!ok && a[i] > b[ind]) {
        continue;
      }
      int mask_ = mask - (1 << i);
      int ok_ = ok || (a[i] < b[ind]);
      ret = ret || (dp(ok_, mask_));
      if (ret == 1) {
        ans = a[i] + ans;
        return ret;
      }
    }
  }
  return ret;
}
void print(int ok, int mask) {
  if (mask == 0) {
    return;
  }
  int ret = mem[ok][mask];
  int ind = 0;
  for (long long i = 0; i <= aa - 1; i++) {
    ind += (!(mask & (1 << i)));
  }
  for (int i = 0; i < aa; i++) {
    if (mask & (1 << i)) {
      if (!ok && a[i] > b[ind]) {
        continue;
      }
      int mask_ = mask - (1 << i);
      int ok_ = ok || (a[i] < b[ind]);
      int tmp = (dp(ok_, mask_));
      if (ret == tmp) {
        printf("%d", (int)a[i] - '0');
        print(ok_, mask_);
        return;
      }
    }
  }
  return;
}
int main() {
  memset(mem, -1, sizeof mem);
  scanf(" %s", a);
  scanf(" %s", b);
  aa = strlen(a);
  bb = strlen(b);
  sort(a, a + aa, greater<char>());
  if (aa < bb) {
    printf("%s\n", a);
    return 0;
  }
  dp(0, (1 << aa) - 1);
  cout << ans << endl;
}
