#include <bits/stdc++.h>   #pragma comment(linker,  /STACK:134217728 )   using namespace std;   const long long MOD = 1000000000 + 7;   const long long MAGIC = 123123123;   const double PI = 4 * atan(1.);   const double EPS = 1E-7;   void time_elapsed() {     cout <<   nTIME ELAPSED:   << (double)clock() / CLOCKS_PER_SEC <<   sec n ;   }   struct frac;   template <typename T>   T gcd(T a, T b) {     return ((b == 0) ? a : gcd(b, a % b));   }   template <class T>   T gcd(T a, T b, T& x, T& y) {     if (!a) {       x = 0, y = 1;       return b;     }     T x1, y1;     T d = gcd(b % a, a, x1, y1);     x = y1 - (b / a) * x1;     y = x1;     return d;   }   template <typename T>   T lcm(T a, T b) {     return (a / gcd(a, b)) * b;   }   template <typename T, typename M>   T neg_mod(T a, M mod) {     return ((a % mod) + mod) % mod;   }   long long binpow(long long x, long long p) {     long long res = 1;     while (p) {       if (p & 1) res *= x;       x *= x;       p >>= 1;     }     return res;   }   long long binpow_mod(long long x, long long p, long long m) {     long long res = 1;     while (p) {       if (p & 1) res = (res * x) % m;       x = (x * x) % m;       p >>= 1;     }     return res;   }   struct cmp_for_set {     bool operator()(const pair<long long, long long>& a,                     const pair<long long, long long>& b) const {       return a > b;     }   };   string a, b;   map<char, int> amap;   bool check(int len, map<char, int>& need, string& res) {     map<char, int> aa = amap;     for (auto it : need) {       if (aa[it.first] < it.second) {         return false;       }       aa[it.first] -= it.second;       if (aa[it.first] == 0) {         aa.erase(it.first);       }     }     res.resize(a.size());     for (int i = 0; i < len; ++i) {       res[i] = b[i];     }     if (len == b.size()) {       return true;     }     char nxt =  0  - 1;     for (auto it : aa) {       if (it.first < b[len] && it.second > 0) {         nxt = it.first;       }     }     if (nxt ==  0  - 1) {       return false;     }     res[len] = nxt;     aa[nxt]--;     vector<char> toadd;     for (auto it : aa) {       for (int j = 0; j < it.second; ++j) {         toadd.push_back(it.first);       }     }     for (int i = 0; i < toadd.size(); ++i) {       res[len + 1 + i] = toadd[(int)toadd.size() - 1 - i];     }     return true;   }   int main() {     cin >> a >> b;     for (int i = 0; i < a.size(); ++i) {       amap[a[i]]++;     }     string res = a;     sort(res.begin(), res.end());     if (a.size() < b.size()) {       sort(res.rbegin(), res.rend());       cout << res;       return 0;     }     map<char, int> need;     for (int i = 0; i <= b.size(); ++i) {       string cur;       if (check(i, need, cur)) {         res = cur;       }       need[b[i]]++;     }     cout << res;     return 0;   }   