#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1E5;
string a, b;
int num[20], len1, len2;
void bfs(int x, ll ans, bool limit) {
  int now = b[x] - '0';
  int k = limit ? now : 9;
  for (int i = k; i >= 0; i--) {
    if (num[i]) {
      if (x == len1 - 1) {
        cout << ans * 10 + i << endl;
        exit(0);
      }
      num[i]--;
      bfs(x + 1, ans * 10 + i, limit && i == now);
      num[i]++;
    }
  }
}
int main() {
  ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
  cin >> a >> b;
  len1 = a.size();
  len2 = b.size();
  for (int i = 0; i < len1; i++) {
    num[a[i] - '0']++;
  }
  if (len1 == 1) {
    cout << a << endl;
    return 0;
  }
  if (len1 < len2) {
    ll ans = 0;
    for (int i = 9; i >= 0; i--) {
      while (num[i]) {
        ans = ans * 10 + i;
        num[i]--;
      }
    }
    cout << ans << endl;
  } else {
    for (int i = b[0] - '0'; i >= 1; i--) {
      if (num[i]) {
        num[i]--;
        bfs(1, i, b[0] - '0' == i);
        num[i]++;
      }
    }
  }
}
