#include <bits/stdc++.h>
#pragma GCC optimize("O3")
const long long INF = 0x3f3f3f3f3f3f3f3f;
const long long llinf = LLONG_MAX;
const int inf = INT_MAX;
const int nmax = 1e5 + 5;
const int mod = 1e9 + 7;
using namespace std;
long long a, b, pw = 1, pw1, cur, cur1, i, j, viz[21], tmp[21], l;
vector<long long> d;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cerr.tie(0);
  cout.tie(0);
  cin >> a >> b;
  while (a) d.push_back(a % 10), a /= 10;
  sort(d.begin(), d.end());
  for (i = 0; i < d.size() - 1; i++) pw *= 10;
  for (i = 0; i < d.size(); i++) {
    for (j = d.size() - 1; j >= 0; j--)
      if (!viz[j]) {
        if (i != d.size() - 1) {
          cur1 = d[j] * pw;
          for (l = 0; l <= 20; l++) tmp[l] = 0;
          tmp[j] = 1;
          pw1 = pw / 10;
          for (l = 0; l < d.size() && pw1; l++)
            if (!viz[l] && !tmp[l]) cur1 += d[l] * pw1, tmp[l] = 1, pw1 /= 10;
          if (cur + cur1 <= b) {
            cur += d[j] * pw;
            viz[j] = 1;
            break;
          }
        } else {
          if (cur + d[j] * pw <= b) {
            cur += d[j] * pw;
            viz[j] = 1;
            break;
          }
        }
      }
    pw /= 10;
  }
  cout << cur << endl;
  return 0;
}
