#include <bits/stdc++.h>
using namespace std;
const int N = 20;
int dp[N][1 << N][2];
string a, b;
string ans;
int solve(int idx = 0, int mask = 0, int flag = 0) {
  if (idx == int(a.size())) return 1;
  int &ret = dp[idx][mask][flag];
  if (~ret) return ret;
  ret = 0;
  for (int i = int(int(a.size()) - 1); i >= 0; i--) {
    if (mask & (1 << i)) continue;
    if (flag) {
      if (solve(idx + 1, mask | (1 << i), 1)) {
        ans += a[i];
        return ret = 1;
      }
    } else {
      if (a[i] <= b[idx]) {
        if (solve(idx + 1, mask | (1 << i), a[i] < b[idx])) {
          ans += a[i];
          return ret = 1;
        }
      }
    }
  }
  return ret;
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  memset(dp, -1, sizeof dp);
  cin >> a >> b;
  if (int(b.size()) > int(a.size())) {
    sort(a.rbegin(), a.rend());
    cout << a << endl;
    return 0;
  }
  sort(a.begin(), a.end());
  solve(0, 0);
  reverse(ans.begin(), ans.end());
  cout << ans << endl;
  return 0;
}
