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