#include <bits/stdc++.h>   using namespace std;   signed main() {     std::ios_base::sync_with_stdio(0), cin.tie(0);     string s, t;     cin >> s >> t;     int a[26] = {};     int n = s.size(), m = t.size();     for (int i = 0; i < n; ++i) a[s[i] -  a ]++;     bool ok = 0;     if (n < m) {       s =   ;       for (int i = 0; i < n && !ok; ++i) {         if (a[t[i] -  a ]) {           a[t[i] -  a ]--;           s += t[i];           continue;         } else {           for (int j = t[i] -  a  + 1; j < 26; ++j) {             if (a[j]) {               a[j]--;               s += char( a  + j);               ok = 1;               break;             }           }           if (ok) break;           for (int j = i - 1; j >= 0 && !ok; --j) {             a[s[j] -  a ]++;             s.pop_back();             for (int k = t[j] -  a  + 1; k < 26; ++k) {               if (a[k]) {                 a[k]--;                 s += char(k +  a );                 ok = 1;                 break;               }             }           }           if (!ok) {             cout << -1;             return 0;           }         }       }       if (!ok) {         cout << -1;         return 0;       }       for (int i = 0; i < 26; ++i) {         for (int j = 0; j < a[i]; ++j) {           s += char( a  + i);         }       }       cout << s;       return 0;     }     s =   ;     for (int i = 0; i < m && !ok; ++i) {       if (a[t[i] -  a ]) {         a[t[i] -  a ]--;         s += t[i];         continue;       } else {         for (int j = t[i] -  a  + 1; j < 26; ++j) {           if (a[j]) {             a[j]--;             s += char( a  + j);             ok = 1;             break;           }         }         if (ok) break;         for (int j = i - 1; j >= 0 && !ok; --j) {           a[s[j] -  a ]++;           s.pop_back();           for (int k = t[j] -  a  + 1; k < 26; ++k) {             if (a[k]) {               a[k]--;               s += char(k +  a );               ok = 1;               break;             }           }         }         if (!ok) {           cout << -1;           return 0;         }       }     }     if (!ok) {       if (n == m) {         if (next_permutation(s.begin(), s.end()))           cout << s;         else           cout << -1;         return 0;       }     }     for (int i = 0; i < 26; ++i) {       for (int j = 0; j < a[i]; ++j) {         s += char( a  + i);       }     }     cout << s;     return 0;   }   