#include <bits/stdc++.h>
using namespace std;
long long a, b, bx, res;
int digA, digB;
int n[10];
bool found = false;
void f(long long x, int d) {
  if (d == digA) {
    if (x <= b) {
      found = true;
      res = x;
    }
    return;
  }
  b *= 10;
  b += bx % 10;
  bx /= 10;
  for (int i = 9; i >= 0; i--) {
    if (n[i] > 0 && x * 10 + i <= b) {
      n[i]--;
      f(x * 10 + i, d + 1);
      if (found) return;
      n[i]++;
    }
  }
  bx *= 10;
  bx += b % 10;
  b /= 10;
}
int main() {
  cin >> a >> b;
  for (int i = 0; i < 10; i++) n[i] = 0;
  while (a > 0) {
    n[a % 10]++;
    a /= 10;
    digA++;
  }
  bx = b;
  while (bx > 0) {
    bx /= 10;
    digB++;
  }
  if (digA < digB) {
    b = 0;
    for (int i = 0; i < digA; i++) {
      b *= 10, b += 9;
    }
    bx = b;
    b = 0;
  } else {
    while (b > 0) {
      bx *= 10;
      bx += b % 10;
      b /= 10;
    }
  }
  f(0, 0);
  cout << res << endl;
  return 0;
}
