#include <bits/stdc++.h>
using namespace std;
long long a, b;
long long ha[22];
long long hb[22];
int cnt = 0;
void dfs(int pos, long long ans, bool able) {
  if (ans > b) return;
  if (pos == cnt) {
    if (able) {
      cout << ans << endl;
      exit(0);
    } else if (ans <= b) {
      cout << ans << endl;
      exit(0);
    }
    return;
  }
  if (able) {
    long long mx = 0;
    for (int i = 1; i < cnt; i++) {
      if (ha[i] > ha[mx]) mx = i;
    }
    long long haha = -1;
    swap(haha, ha[mx]);
    dfs(pos + 1, ans * 10 + haha, 1);
    swap(haha, ha[mx]);
    return;
  }
  long long bound = hb[pos];
  long long sav[22];
  for (int i = 0; i < cnt; i++) sav[i] = ha[i];
  sort(ha, ha + cnt);
  long long i = upper_bound(ha, ha + cnt, bound) - ha - 1;
  if (i >= cnt) {
    for (int i = 0; i < cnt; i++) ha[i] = sav[i];
    return;
  }
  long long haha = -1;
  for (; i >= 0; i--) {
    if (ha[i] == -1) break;
    if (ha[i] == 0 && pos == 0) break;
    if (ha[i] == ha[i + 1]) continue;
    swap(haha, ha[i]);
    dfs(pos + 1, ans * 10 + haha, (haha < bound));
    swap(haha, ha[i]);
  }
  for (int i = 0; i < cnt; i++) ha[i] = sav[i];
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cin >> a >> b;
  while (a > 0) {
    ha[cnt] = a % 10;
    cnt++;
    a /= 10;
  }
  long long mx = 0;
  sort(ha, ha + cnt);
  for (int i = cnt - 1; i >= 0; i--) mx = mx * 10 + ha[i];
  if (mx <= b) {
    cout << mx << endl;
    return 0;
  }
  ha[cnt] = 2333;
  long long sav = b;
  cnt = 0;
  while (b > 0) {
    hb[cnt] = b % 10;
    cnt++;
    b /= 10;
  }
  reverse(hb, hb + cnt);
  b = sav;
  dfs(0, 0LL, 0);
  return 0;
}
