#include <bits/stdc++.h>
using namespace std;
char num1[20];
char num2[20];
bool calc(int pos, int len) {
  if (pos == len) return true;
  sort(num1 + pos, num1 + len, greater<char>());
  bool res = false;
  for (int i = pos; i < len && !res; i++) {
    if (num1[i] == num2[pos]) {
      swap(num1[i], num1[pos]);
      sort(num1 + pos + 1, num1 + len);
      if (strcmp(num1, num2) <= 0)
        res = res || calc(pos + 1, len);
      else
        sort(num1 + pos, num1 + len, greater<char>());
    } else if (num1[i] < num2[pos]) {
      swap(num1[i], num1[pos]);
      sort(num1 + pos + 1, num1 + len, greater<char>());
      return true;
    }
  }
  return res;
}
int main(int argc, char* argv[]) {
  cin >> num1 >> num2;
  int len1 = strlen(num1);
  int len2 = strlen(num2);
  if (len1 < len2) {
    sort(num1, num1 + len1, greater<char>());
    cout << num1 << endl;
  } else {
    calc(0, len1);
    cout << num1 << endl;
  }
  return 0;
}
