#include <bits/stdc++.h>
using namespace std;
vector<int> num1, num2;
string s, s1;
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> s >> s1;
  for (int i = 0; i < s.size(); i++) num1.push_back(s[i] - '0');
  for (int i = 0; i < s1.size(); i++) num2.push_back(s1[i] - '0');
  sort(num1.begin(), num1.end());
  reverse(num1.begin(), num1.end());
  if (num2.size() > num1.size()) {
    for (int i = 0; i < num1.size(); i++) cout << num1[i];
    return 0;
  }
  bool b = 0, k = 0;
  for (int i = 0; i < num1.size(); i++) {
    if (b == 1) {
      sort(num1.begin() + i, num1.end());
      reverse(num1.begin() + i, num1.end());
      break;
    }
    if (k) {
      int maxx = 0;
      for (int h = i; h < num1.size(); h++)
        if (num1[h] < num2[i]) maxx = max(maxx, num1[h]);
      for (int j = i; j < num1.size(); j++) {
        if (num1[j] == maxx) {
          long long x = num1[j];
          for (int h = j; h > i; h--) num1[h] = num1[h - 1];
          num1[i] = x;
          if (x < num2[i]) b = 1;
          break;
        }
      }
      k = 0;
      if (b == 0) {
        i--;
        i--;
        k = 1;
      }
      continue;
    }
    if (num1[i] < num2[i]) {
      int maxx = 0;
      for (int h = i; h < num1.size(); h++)
        if (num1[h] <= num2[i]) maxx = max(maxx, num1[h]);
      for (int j = i; j < num1.size(); j++) {
        if (num1[j] == maxx) {
          long long x = num1[j];
          for (int h = j; h > i; h--) num1[h] = num1[h - 1];
          num1[i] = x;
          if (x < num2[i]) b = 1;
          break;
        }
      }
      b = 1;
    }
    if (num1[i] > num2[i]) {
      int maxx = 0;
      for (int h = i + 1; h < num1.size(); h++)
        if (num1[h] <= num2[i]) maxx = max(maxx, num1[h]);
      for (int j = i; j < num1.size(); j++) {
        if (num1[j] == maxx) {
          long long x = num1[j];
          for (int h = j; h > i; h--) num1[h] = num1[h - 1];
          num1[i] = x;
          if (x < num2[i]) b = 1;
          break;
        }
      }
    }
    if (num2[i] < num1[i] && num2[i - 1] == num1[i - 1] && b == 0) {
      k = 1;
      i--;
      i--;
    }
  }
  for (int i = 0; i < num1.size(); i++) cout << num1[i];
  return 0;
}
