#include <bits/stdc++.h>
using namespace std;
struct Matrix {
  vector<vector<int>> data;
  int r, c;
  Matrix(int row, int col, bool identity = false) : r(row), c(col) {
    data.assign(row, vector<int>(col, 0));
    if (identity) {
      for (int i = 0; i < r; ++i) {
        data[i][i] = 1;
      }
    }
  }
  Matrix operator*(Matrix& other) {
    int m = r, n = c, p = other.c;
    Matrix res(m, p);
    for (int i = 0; i < m; ++i) {
      for (int j = 0; j < p; ++j) {
        for (int k = 0; k < n; ++k) {
          res.data[i][j] += data[i][k] * other.data[k][j];
        }
      }
    }
    return res;
  }
};
vector<int> extendGcd(int a, int b) {
  if (b == 0) {
    return {a, 1, 0};
  } else {
    vector<int> tmp = extendGcd(b, a % b);
    return {tmp[0], tmp[2], tmp[1] - (a / b) * tmp[2]};
  }
}
Matrix matrix_power(Matrix base, long long exp) {
  int n = base.r;
  Matrix res(n, n, true);
  while (exp) {
    if (exp & 1) {
      res = res * base;
    }
    base = base * base;
    exp >>= 1;
  }
  return res;
}
template <typename T>
T power_mod(T base, int exp, int mod = 1000000007) {
  T res = 1;
  while (exp) {
    if (exp & 1) res = res * base % mod;
    base = base * base % mod;
    exp >>= 1;
  }
  return res;
}
template <typename T>
T power(T base, int exp) {
  T res = 1;
  while (exp) {
    if (exp & 1) res *= base;
    base *= base;
    exp >>= 1;
  }
  return res;
}
long long inv(long long a) { return power_mod(a, 1000000007 - 2); }
string a, b;
string res;
vector<int> cnt(10, 0);
bool dfs(int index, bool small) {
  if (index == a.length()) return true;
  if (small) {
    for (int i = 9; i >= 0; --i) {
      if (cnt[i]) {
        res.push_back('0' + i);
        --cnt[i];
        return dfs(index + 1, small);
      }
    }
  } else {
    if (cnt[b[index] - '0']) {
      res.push_back(b[index]);
      --cnt[b[index] - '0'];
      if (dfs(index + 1, false)) return true;
      ++cnt[b[index] - '0'];
      res.pop_back();
    }
    for (int i = b[index] - '0' - 1; i >= 0; --i) {
      if (cnt[i]) {
        res.push_back('0' + i);
        --cnt[i];
        return dfs(index + 1, true);
      }
    }
    return false;
  }
}
int main() {
  cin >> a >> b;
  if (a.length() < b.length()) {
    sort((a).begin(), (a).end(), greater<char>());
    cout << a;
  } else {
    for (char ch : a) {
      ++cnt[ch - '0'];
    }
    dfs(0, false);
    cout << res;
  }
  return 0;
}
