#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ull = unsigned long long;
const ll MAXN = 1e9 + 1;
bool trynum(int i, int num, string as, ll b) {
  as[i] = (char)num + '0';
  ll a = stoll(as);
  if (a <= b) {
    return true;
  }
  return false;
}
void solveC() {
  ios::sync_with_stdio(false);
  cin.tie(NULL);
  ll a, b;
  cin >> a >> b;
  if (a == b) {
    cout << a;
    return;
  }
  string as = to_string(a);
  string bs = to_string(b);
  map<int, int> anum;
  for (int i = 0; i < as.size(); i++) {
    anum[as[i] - '0']++;
  }
  string ans;
  ans.resize(as.size(), '0');
  if (as.size() == bs.size()) {
    for (int i = 0; i < as.size(); i++) {
      for (int i1 = 9; i1 >= 0; i1--) {
        if (anum[i1] != 0 && trynum(i, i1, ans, b)) {
          ans[i] = (char)i1 + '0';
          anum[i1]--;
          break;
        }
        if (i1 == 0) {
          bool flag = true;
          while (flag) {
            --i;
            int last_num = ans[i] - '0';
            anum[last_num]++;
            ans[i] = '0';
            if (last_num == 0) {
              continue;
            }
            for (int i1 = last_num - 1; i1 >= 0; i1--) {
              if (anum[i1] != 0 && trynum(i, i1, ans, b)) {
                ans[i] = (char)i1 + '0';
                anum[i1]--;
                flag = false;
                break;
              }
            }
          }
        }
      }
    }
  } else {
    for (int i = 0; i < as.size(); i++) {
      for (int i1 = 9; i1 >= 0; i1--) {
        if (anum[i1] != 0) {
          ans[i] = (char)i1 + '0';
          anum[i1]--;
          break;
        }
      }
    }
  }
  cout << ans;
}
int main() {
  solveC();
  return 0;
}
