#include <bits/stdc++.h>
using namespace std;
vector<int> x[2];
int main() {
  long long a, b;
  cin >> a >> b;
  while (a) {
    x[0].push_back(a % 10);
    a /= 10;
  }
  while (b) {
    x[1].push_back(b % 10);
    b /= 10;
  }
  reverse(x[1].begin(), x[1].end());
  sort(x[0].begin(), x[0].end(), greater<int>());
  if (x[1].size() > x[0].size()) {
    for (int i = 0; i < x[0].size(); i++) printf("%d", x[0][i]);
    return 0;
  }
  for (int i = 0; i < x[0].size(); i++) {
    int is = 1;
    int dex = i;
    if (x[0][i] > x[1][i]) dex = -1;
    for (int j = i + 1; j < x[0].size(); j++) {
      if (x[0][j] <= x[1][i]) {
        if (dex == -1 || x[0][dex] < x[0][j]) dex = j;
      }
    }
    if (dex != -1) swap(x[0][i], x[0][dex]);
    if (x[0][i] > x[1][i]) is = 0;
    if (is == 0) {
      for (int j = i - 1; j >= 0; j--) {
        int cur = -1;
        for (int k = j + 1; k < x[0].size(); k++) {
          if (x[0][k] < x[0][j]) {
            if (cur == -1 || x[0][k] > x[0][cur]) cur = k;
          }
        }
        if (cur != -1) {
          swap(x[0][cur], x[0][j]);
          sort(x[0].begin() + j + 1, x[0].end(), greater<int>());
          i = x[0].size() - 1;
          break;
        }
      }
    }
    if (x[0][i] < x[1][i] && i + 1 != x[0].size()) {
      sort(x[0].begin() + i + 1, x[0].end(), greater<int>());
      break;
    }
  }
  for (int i = 0; i < x[0].size(); i++) printf("%d", x[0][i]);
}
