#include <bits/stdc++.h>   using namespace std;   int cnt[125];   char sa[5005];   int main() {     bool ok, whh;     int lens, lent;     string s, t, my;     cin >> s >> t;     memset(cnt, 0, sizeof cnt);     lens = s.length();     for (int i = 0; i < lens; i++) cnt[s[i]]++;     ok = true;     whh = false;     my = s;     lent = t.length();     if (lens <= lent) {       for (int i = 0; i < lens; i++) {         char c = t[i];         if (whh) {           c =  a ;           while (c <=  z )             if (cnt[c]) {               cnt[c]--;               break;             } else               c++;           my[i] = c;           continue;         }         if (!ok) {           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }         while (c <=  z )           if (cnt[c]) {             cnt[c]--;             break;           } else             c++;         if (c == t[i])           my[i] = c;         else if (c <=  z ) {           my[i] = c;           whh = true;         } else {           ok = false;           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }       }       if (my == t) {         strcpy(sa, my.c_str());         if (!next_permutation(sa, sa + lens))           ok = false;         else {           string tmp(sa);           my = tmp;         }       } else if (my < t) {         strcpy(sa, my.c_str());         while (next_permutation(sa, sa + lens)) {           string tmp(sa);           if (tmp > t) {             my = tmp;             break;           }         }         if (my <= t)           ok = false;         else           ok = true;       }     } else {       for (int i = 0; i < lens; i++) {         char c = t[i];         if (whh) {           c =  a ;           while (c <=  z )             if (cnt[c]) {               cnt[c]--;               break;             } else               c++;           my[i] = c;           continue;         }         if (!ok) {           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }         while (c <=  z )           if (cnt[c]) {             cnt[c]--;             break;           } else             c++;         if (c == t[i])           my[i] = c;         else if (c <=  z ) {           my[i] = c;           whh = true;         } else {           ok = false;           c =  z ;           while (c >=  a )             if (cnt[c]) {               cnt[c]--;               break;             } else               c--;           my[i] = c;           continue;         }       }       if (my == t) {         strcpy(sa, my.c_str());         if (!next_permutation(sa, sa + lens))           ok = false;         else {           string tmp(sa);           my = tmp;         }       } else if (my < t) {         strcpy(sa, my.c_str());         while (next_permutation(sa, sa + lens)) {           string tmp(sa);           if (tmp > t) {             my = tmp;             break;           }         }         if (my <= t)           ok = false;         else           ok = true;       }     }     if (ok)       cout << my << endl;     else       cout <<  -1  << endl;     return 0;   }