#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;
const int INF = 1 << 29;
const ll LL_INF = 1LL << 60;
const double EPS = 1e-9;
const ll MOD = 1000000007;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, -1, 0, 1};
ll A, B;
string SA, SB;
int N;
map<int, int> cnts;
bool check(string tmp, map<int, int> rest) {
  for (int i = 0; i <= 9; i++) {
    for (int j = 0; j < rest[i]; j++) {
      tmp += to_string(i);
    }
  }
  return tmp <= SB;
}
int main() {
  cin >> A >> B;
  SA = to_string(A);
  SB = to_string(B);
  if (SA.size() < SB.size()) {
    sort(SA.rbegin(), SA.rend());
    cout << SA << endl;
    return 0;
  }
  N = SA.size();
  for (int i = 0; i < N; i++) {
    cnts[SA[i] - '0']++;
  }
  string res = "";
  for (int i = 0; i < N; i++) {
    bool ok = false;
    for (char j = 9; j >= 0; j--) {
      if (cnts[j] > 0) {
        string tmp = res + to_string(j);
        map<int, int> rest = cnts;
        rest[j]--;
        if (check(tmp, rest)) {
          ok = true;
          cnts[j]--;
          res = tmp;
          break;
        }
      }
    }
  }
  cout << res << endl;
  return 0;
}
