#include <bits/stdc++.h>
using namespace std;
long long solve(string A, string B) {
  vector<string> ans;
  for (int x = 0; x < (B.size()); x++) {
    map<char, int> mymap;
    for (int i = 0; i < (A.size()); i++) mymap[A[i]]++;
    for (int xx = 0; xx < (x); xx++)
      if (mymap[B[xx]] <= 0)
        goto end;
      else
        mymap[B[xx]]--;
    for (auto c : mymap) {
      if (c.second <= 0) continue;
      map<char, int> mymap2(mymap);
      mymap2[c.first]--;
      string U;
      for (auto t : mymap2)
        for (int y = 0; y < (t.second); y++) U += t.first;
      sort(U.begin(), U.end(), greater<char>());
      string Ans;
      for (int l = 0; l < (x); l++) Ans.push_back(B[l]);
      Ans += c.first;
      Ans += U;
      assert(Ans.length() == A.length());
      ans.push_back(Ans);
    }
  }
end:
  ans.push_back(A);
  sort(ans.begin(), ans.end());
  ans.resize(distance(ans.begin(), unique(ans.begin(), ans.end())));
  long long b = stoull(B);
  long long answer = -1;
  for (auto s : ans) {
    if (s.length() != A.length()) continue;
    if (s[0] == '0') continue;
    long long first = stoull(s);
    if (first <= b) answer = max(answer, first);
  }
  return answer;
}
int main(int argc, char *argv[]) {
  cout.sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cerr.tie(0);
  cout << setprecision(14);
  string A, B;
  while (cin >> A >> B) {
    cout << solve(A, B) << endl;
  }
}
