#include <bits/stdc++.h>
using namespace std;
bool vis[20][10];
bool used[20];
int A[20];
int B[20];
int C[20];
int cnt;
long long a, b;
long long ans;
bool cmp(int a, int b) { return a > b; }
int digit(long long a) {
  int cur = 0;
  long long temp = a;
  while (temp) {
    temp /= 10;
    cur++;
  }
  temp = a;
  int cnt = cur;
  while (temp) {
    C[cur--] = temp % 10;
    temp /= 10;
  }
  return cnt;
}
long long change(int D[]) {
  long long ret = 0, temp = 1;
  int cur = cnt;
  int tot = 0;
  for (int i = 1; i <= cnt; i++) {
    if (!used[i]) {
      tot++;
      D[cur--] = A[i];
    }
  }
  for (int i = cnt; i >= 1; i--) {
    ret += temp * D[i];
    temp *= 10;
  }
  return ret;
}
void dfs(int state, bool flag) {
  if (state == cnt + 1) {
    long long ret = 0, temp = 1;
    for (int i = cnt; i >= 1; i--) {
      ret += temp * C[i];
      temp *= 10;
    }
    if (ret <= b) {
      ans = max(ans, ret);
    }
    return;
  }
  bool dflag;
  for (int i = 1; i <= cnt; i++) {
    if (state == 1 && A[i] > B[1]) continue;
    if (flag && A[i] > B[state]) continue;
    if (!used[i]) {
      C[state] = A[i];
      used[i] = true;
      long long ret = change(C);
      dflag = flag;
      if (ret > b || ret <= ans) {
        used[i] = false;
        continue;
      }
      if (state == 1) {
        if (A[i] == B[1])
          flag = true;
        else
          flag = false;
      }
      if (state > 1 && flag) {
        if (A[i] == B[state])
          flag = true;
        else
          flag = false;
      }
      dfs(state + 1, flag);
      flag = dflag;
      used[i] = false;
    }
  }
}
int main() {
  while (scanf("%I64d%I64d", &a, &b) != EOF) {
    memset(C, 0, sizeof(C));
    int len = digit(a);
    cnt = len;
    for (int i = 1; i <= len; i++) A[i] = C[i];
    sort(A + 1, A + cnt + 1, cmp);
    len = digit(b);
    for (int i = 0; i <= len; i++) B[i] = C[i];
    if (cnt < len) {
      long long ret = 0, temp = 1;
      for (int i = cnt; i >= 1; i--) {
        ret += A[i] * temp;
        temp *= 10;
      }
      printf("%I64d\n", ret);
      continue;
    }
    memset(C, 0, sizeof(C));
    memset(vis, false, sizeof(vis));
    ans = 0;
    memset(used, false, sizeof(used));
    dfs(1, false);
    printf("%I64d\n", ans);
  }
  return 0;
}
