#include <bits/stdc++.h>
using namespace std;
struct point {
  long double x, y;
  point() {
    x = 0;
    y = 0;
  }
  point(long long a, long long b) {
    x = a;
    y = b;
  }
  bool operator==(const point& b) { return x == b.x and y == b.y; }
  bool operator>(const point& b) {
    if (x > b.x) return true;
    if (x < b.x) return false;
    if (x == b.x) {
      if (y < b.y) return false;
      if (y > b.y) return true;
      return true;
    }
  }
  bool operator<(point& b) { return (b > point(x, y)); }
  point(long double x1, long double y1) {
    x = x1;
    y = y1;
  }
  long double dist(point b) {
    return sqrt((b.x - x) * (b.x - x) + (y - b.y) * (y - b.y));
  }
  long double dist_kv(point b) {
    return ((b.x - x) * (b.x - x) + (y - b.y) * (y - b.y));
  }
  bool point_on_sig(point& a, point& b) {
    return abs(a.dist(b) - (dist(a) + dist(b))) <= 1e-6;
  }
  long double polar_angle() {
    long double alpha = atan2(y, x);
    long double pi = 3.141592654;
    if (alpha < 0) alpha += 2 * pi;
    return alpha;
  }
};
struct line {
  long double A, B, C;
  line() {
    A = 0;
    B = 0;
    C = 0;
  }
  line(point& a, point& b) {
    A = a.y - b.y;
    B = b.x - a.x;
    C = -A * a.x - B * a.y;
  }
  line(long double a, long double b, long double c) {
    A = a;
    B = b;
    C = c;
  }
  line line_perp_dan_cherez_point(point a) {
    return line(-B, A, -(A * a.y) + B * a.x);
  }
  bool onli(point a) { return A * a.x + B * a.y + C == 0; }
  point intersect_lines(line& a) {
    point p;
    p.x = -(((C * a.B) - (a.C * B)) / ((A * a.B) - (a.A * B)));
    p.y = -(((A * a.C) - (a.A * C)) / ((A * a.B) - (a.A * B)));
    return p;
  }
  bool intersect(line& b) { return A * b.B - B * b.A != 0; }
  bool operator==(line& a) { return A == a.A && B == a.B && C == a.C; }
  long double distance_perpendicular(point& d) {
    return abs(A * d.x + B * d.y + C) / (sqrt(A * A + B * B));
  }
};
long long INF = (long long)1e20;
struct node {
  long long x, y, sz;
  node* l;
  node* r;
  node(long long xx, long long yy) {
    x = xx;
    sz = 1;
    y = yy;
    l = nullptr;
    r = nullptr;
  }
};
long long sz(node* r) { return (r != nullptr) ? r->sz : 0; }
void upd_sz(node* r) {
  if (r != nullptr) r->sz = 1 + sz(r->l) + sz(r->r);
}
node* merge(node* a, node* b) {
  if (a == nullptr) {
    upd_sz(a);
    return b;
  }
  if (b == nullptr) {
    upd_sz(a);
    return a;
  }
  if (a->y > b->y) {
    node* x = merge(a->r, b);
    a->r = x;
    upd_sz(a);
    return a;
  }
  node* x = merge(a, b->l);
  b->l = x;
  upd_sz(b);
  return b;
}
pair<node*, node*> split(node* r, long long x) {
  if (r == nullptr) {
    return {nullptr, nullptr};
  }
  if (sz(r->l) >= x) {
    auto p = split(r->l, x);
    r->l = p.second;
    upd_sz(r);
    upd_sz(p.first);
    return {p.first, r};
  }
  if (sz(r->l) + 1 == x) {
    auto p = r->r;
    r->r = nullptr;
    upd_sz(r);
    upd_sz(p);
    return {r, p};
  }
  auto p = split(r->r, x - sz(r->l) - 1);
  r->r = p.first;
  upd_sz(r);
  upd_sz(p.second);
  return {r, p.second};
}
void print_kurevo(node* r) {
  if (r == nullptr) return;
  print_kurevo(r->l);
  cout << r->x << ' ';
  print_kurevo(r->r);
}
void print_kurevo1(node* r) {
  if (r == nullptr) return;
  cout << "START " << r->x << '\n';
  print_kurevo1(r->l);
  print_kurevo1(r->r);
  cout << "END " << r->x << '\n';
}
int main() {
  string a, b;
  cin >> a >> b;
  if (b.size() > a.size()) {
    sort(a.rbegin(), a.rend());
    cout << a;
    return 0;
  }
  multiset<char> ms;
  for (auto i : a) ms.insert(i);
  string ans;
  long long can = -1;
  string thens;
  multiset<char> thenm;
  bool built = true;
  for (int i = 0; i < b.size(); ++i) {
    char mm = 'K';
    for (auto j : ms) {
      if (j <= b[i]) mm = j;
    }
    if (mm == 'K') {
      mm = 'K';
      for (auto j : thenm) {
        if (j < b[can]) mm = j;
      }
      thens += mm;
      thenm.erase(thenm.find(mm));
      cout << thens;
      string aaa;
      for (auto j : thenm) {
        aaa += j;
      }
      sort(aaa.rbegin(), aaa.rend());
      cout << aaa;
      return 0;
    }
    if (mm > (*ms.begin())) {
      can = i;
      thens = ans;
      thenm = ms;
    }
    ans += mm;
    ms.erase(ms.find(mm));
    if (mm < b[i]) {
      cout << ans;
      string aaa;
      for (auto j : ms) {
        aaa += j;
      }
      sort(aaa.rbegin(), aaa.rend());
      cout << aaa;
      return 0;
    }
  }
  cout << ans;
}
