#include <bits/stdc++.h>
using namespace std;
string s, t, ans;
int snum[26];
int cnt[5001][26];
int main() {
  cin >> s >> t;
  ans = s;
  int len1 = s.size();
  int len2 = t.size();
  for (int i = 0; i < len1; i++) snum[s[i] - 'a']++;
  for (int i = 1; i <= len2; i++)
    for (int j = 0; j < 26; j++) {
      if (j == t[i - 1] - 'a')
        cnt[i][j] = cnt[i - 1][j] + 1;
      else
        cnt[i][j] = cnt[i - 1][j];
    }
  if (len1 > len2) {
    int judge = 1;
    for (int i = 0; i < 26; i++)
      if (cnt[len2][i] > snum[i]) judge = 0;
    if (judge == 1) {
      for (int i = 0; i < len2; i++) {
        cout << t[i];
        snum[t[i] - 'a']--;
      }
      for (int i = 0; i < 26; i++)
        if (snum[i] != 0) {
          cout << (char)(i + 'a');
          snum[i]--;
          i--;
        }
      return 0;
    }
  }
  for (int i = len2 - 1; i >= 0; i--) {
    int judge, temp = t[i] - 'a';
    for (int j = temp + 1; j < 26; j++) {
      judge = 1;
      if (snum[j] == 0)
        judge = 0;
      else {
        for (int k = 0; k < 26; k++)
          if (k != j && cnt[i][k] > snum[k]) judge = 0;
        if (cnt[i][j] > snum[j] - 1) judge = 0;
      }
      if (judge == 1) {
        ans[i] = j + 'a';
        snum[j]--;
        for (int k = 0; k < i; k++) {
          ans[k] = t[k];
          snum[t[k] - 'a']--;
        }
        for (int k = 0; k <= i; k++) cout << ans[k];
        for (int k = 0; k < 26; k++)
          if (snum[k] != 0) {
            cout << (char)(k + 'a');
            snum[k]--;
            k--;
          }
        cout << endl;
        return 0;
      }
    }
  }
  cout << "-1" << endl;
  return 0;
}
