#include <bits/stdc++.h>
using namespace std;
void optimise() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
}
void solve() {
  string s;
  cin >> s;
  string s2;
  cin >> s2;
  sort(s.begin(), s.end());
  for (long long int i = 0; i < s.size(); i++) {
    for (long long int j = i + 1; j < s.size(); j++) {
      string k = s;
      swap(k[i], k[j]);
      if (stoll(k) >= stoll(s) && stoll(k) <= stoll(s2)) s = k;
    }
  }
  cout << s;
}
signed main() {
  optimise();
  long long int t = 1;
  while (t--) {
    solve();
  }
}
