#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 5;
const long long INF = 2e18 + 7;
string sa, sb;
long long dp[19][1 << 19];
int len;
long long power[20];
inline long long atoi(string &str) {
  long long ans = 0;
  for (auto it : str) {
    ans = ans * 10 + it - '0';
  }
  return ans;
}
long long fun(int p, int mask) {
  if (p < 0) return 0;
  if (dp[p][mask] != -1) return dp[p][mask];
  long long ans = -INF, mx = -1, eq = -1;
  for (int i = 0; i < len; i++) {
    if (mask & (1 << i)) continue;
    if (sa[i] == sb[p])
      eq = i;
    else if (sa[i] < sb[p] && (mx == -1 || sa[i] > sa[mx])) {
      mx = i;
    }
  }
  if (eq != -1)
    ans = max(ans, (sa[eq] - '0') * power[p] + fun(p - 1, mask | (1 << eq)));
  string v;
  if (mx == -1) goto L1;
  for (int i = 0; i < len; i++) {
    if (i == mx || mask & (1 << i)) continue;
    v.push_back(sa[i]);
  }
  sort(v.begin(), v.end(), greater<char>());
  ans = max(ans, (sa[mx] - '0') * power[p] + atoi(v));
L1:
  return dp[p][mask] = ans;
}
int main() {
  ios::sync_with_stdio(0);
  cin.tie(0);
  power[0] = 1;
  for (int i = 1; i < 20; i++) {
    power[i] = 10 * power[i - 1];
  }
  long long a, b;
  cin >> a >> b;
  sa = to_string(a);
  sb = to_string(b);
  int la = sa.length();
  int lb = sb.length();
  if (la < lb) {
    sort(sa.begin(), sa.end(), greater<char>());
    cout << sa << "\n";
    return 0;
  }
  memset(dp, -1, sizeof dp);
  reverse(sb.begin(), sb.end());
  len = la;
  long long ans = fun(len - 1, 0);
  cout << ans << "\n";
  return 0;
}
