#include <bits/stdc++.h>
using namespace std;
int main() {
  string a, b;
  cin >> a >> b;
  sort(a.begin(), a.end());
  for (int i = 0; i < a.size(); i++)
    for (int j = i + 1; j < a.size(); j++) {
      string str = a;
      swap(str[i], str[j]);
      if (stoll(str) > stoll(a) && stoll(str) <= stoll(b)) a = str;
    }
  cout << a << endl;
  return 0;
}
