#include <bits/stdc++.h>
using namespace std;
int s[10];
bool flag = 0;
string a, b, ans;
inline void dfs(int x, string now) {
  if (x == a.length()) {
    cout << now;
    exit(0);
  }
  for (register int j = 9; j >= 0; --j)
    if (s[j] && (now + char(j + '0') <= b))
      --s[j], dfs(x + 1, now + char(j + '0')), ++s[j];
}
int main() {
  cin >> a >> b;
  if (a.length() < b.length()) {
    sort(a.begin(), a.end(), greater<int>());
    cout << a;
    return 0;
  }
  for (int i = 0; i < a.length(); ++i) ++s[a[i] - '0'];
  dfs(0, "");
  return 0;
}
