#include <bits/stdc++.h>
using namespace std;
char s[25];
int a[25];
long long b;
bool vis[25];
bool check1(long long sum, int len) {
  for (int i = len - 1; i >= 0; i--) {
    if (vis[i]) continue;
    sum = sum * 10LL + a[i];
  }
  if (sum > b)
    return true;
  else
    return false;
}
long long dfs(int dis, long long sum, int len) {
  if (check1(sum, len)) return 0LL;
  if (dis > len) return sum;
  long long Max = 0LL;
  for (int i = 0; i < len; i++) {
    if (!vis[i]) {
      vis[i] = true;
      Max = max(Max, dfs(dis + 1, sum * 10LL + a[i], len));
      vis[i] = false;
      if (Max != 0) return Max;
    }
  }
  return 0;
}
void print(long long ans, int len) {
  long long tmp = ans;
  int cnt;
  while (tmp != 0) {
    cnt++;
    tmp /= 10;
  }
  for (int i = 1; i <= len - cnt; i++) printf("0");
  printf("%I64d\n", ans);
}
int main() {
  while (~scanf("%s %I64d", s, &b)) {
    int len = strlen(s);
    for (int i = 0; i < len; i++) a[i] = s[i] - '0';
    sort(a, a + len);
    reverse(a, a + len);
    long long ans = dfs(1, 0LL, len);
    print(ans, len);
  }
  return 0;
}
