#include <bits/stdc++.h>
using namespace std;
int Compare(string s, string t) {
  if (s == t) return 0;
  if (s.length() > t.length()) return 1;
  if (s.length() < t.length()) return -1;
  return s > t ? 1 : -1;
}
string remC(string s, int pos1, int pos2) {
  string ret = "";
  bool flag = false;
  for (int i = 0; i < pos1; i++) {
    ret += s[i];
  }
  ret += s[pos2];
  for (int i = pos1; i < s.length(); i++) {
    if (i != pos2) ret += s[i];
  }
  return ret;
}
int main() {
  string s, t, tmp, ans;
  cin >> s >> t;
  sort(s.begin(), s.end());
  ans = s;
  for (int i = 0; i < s.length(); i++) {
    for (int j = s.length() - 1; j > i; j--) {
      tmp = ans;
      tmp = remC(tmp, i, j);
      if (Compare(tmp, t) <= 0 && tmp[0] != '0') {
        ans = max(ans, tmp);
        break;
      }
    }
    s = ans;
  }
  cout << ans;
}
