#include <bits/stdc++.h>   using namespace std;   int num[27];   int num2[27];   inline int idx(char a) { return a -  a ; }   char sa[5555], sb[5555], sans1[5555], sans2[5555], sans[5555];   inline char rev(int id) { return  a  + id; }   int lena, lenb;   bool ans1, ans2;   int main() {     scanf( %s , sa);     scanf( %s , sb);     ans1 = false;     ans2 = false;     memset(num, 0, sizeof(num));     memset(num2, 0, sizeof(num2));     lena = strlen(sa);     lenb = strlen(sb);     for (int i = 0; i < lena; i++) {       num[idx(sa[i])]++;       num2[idx(sa[i])]++;     }     if (lena > lenb) {       int flag = 1;       for (int i = 0; i < lenb; i++) {         sans1[i] = sb[i];         if (!num[idx(sb[i])]) {           flag = 0;           break;         } else {           num[idx(sb[i])]--;         }       }       if (flag) {         ans1 = true;         int tl = lenb;         for (int i = 0; i < 26; i++) {           while (num[i]) {             num[i]--;             sans1[tl++] = rev(i);           }         }         sans1[tl] =   0 ;       }     }     for (int i = 0; i < 26; i++) num[i] = num2[i];     int lastmch = -1;     for (int i = 0; i < lenb; i++) {       if (num[idx(sb[i])]) {         num[idx(sb[i])]--;         sans2[i] = sb[i];         lastmch = i;       } else {         int tl = i;         int fg = 0;         for (int j = idx(sb[i]) + 1; j < 26; j++) {           if (num[j]) {             fg = 1;             num[j]--;             sans2[tl++] = rev(j);             break;           }         }         if (!fg) {           for (int k = i - 1; k >= 0; k--) {             tl = k;             if (tl >= 0) {               num[idx(sb[k])]++;               for (int j = idx(sb[k]) + 1; j < 26; j++) {                 if (num[j]) {                   lastmch = k - 1;                   fg = 1;                   num[j]--;                   sans2[tl++] = rev(j);                   break;                 }               }             }             if (fg) break;           }         }         if (fg) {           ans2 = true;           for (int j = 0; j < 26; j++) {             while (num[j]) {               num[j]--;               sans2[tl++] = rev(j);             }           }         }         sans2[tl] =   0 ;         break;       }     }     if (lastmch == lenb - 1 && ans2 == 0) {       int i = lenb;       int fg = 0;       int tl;       for (int k = i - 1; k >= 0; k--) {         tl = k;         if (tl >= 0) {           num[idx(sb[k])]++;           for (int j = idx(sb[k]) + 1; j < 26; j++) {             if (num[j]) {               lastmch = k - 1;               fg = 1;               num[j]--;               sans2[tl++] = rev(j);               break;             }           }         }         if (fg) break;       }       if (fg) {         ans2 = true;         for (int j = 0; j < 26; j++) {           while (num[j]) {             num[j]--;             sans2[tl++] = rev(j);           }         }       }       sans2[tl] =   0 ;     }     int fg = 0;     if (ans1 == false && ans2 == false) {       cout << -1 << endl;       return 0;     }     if (ans1) {       fg = 1;       strcpy(sans, sans1);     }     if (fg && ans2) {       if (strcmp(sans1, sans2) > 0) {         strcpy(sans, sans2);       } else {         strcpy(sans, sans1);       }     } else if (ans2) {       strcpy(sans, sans2);     }     puts(sans);     return 0;   }