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