#include <bits/stdc++.h>
using namespace std;
char str1[5050], str2[5050];
int h[30];
int len, i, j;
bool same(char*, char*);
void ist(char*, char);
void del(char*);
bool workit();
int main() {
  memset(str1, 0, sizeof(str1));
  memset(str2, 0, sizeof(str2));
  cin >> str1 >> str2;
  len = strlen(str1);
  memset(h, 0, sizeof(h));
  for (i = 0; i < len; i++) h[str1[i] - 'a' + 1]++;
  if (!workit()) cout << -1 << endl;
}
bool big(char* str1, char* str2) {
  int len, len1, i;
  len = strlen(str1);
  len1 = strlen(str2);
  int min = len;
  if (min > len1) min = len;
  for (i = 0; i < min; i++) {
    if (str1[i] > str2[i]) return true;
    if (str1[i] < str2[i]) return false;
  }
  if (len > len1) return true;
  return false;
}
bool workit() {
  int i, l, stt;
  bool find, bigout;
  char ans[5050];
  memset(ans, 0, sizeof(ans));
  l = 0;
  stt = 1;
  bigout = false;
  while (l < len) {
    find = false;
    for (i = 1; i <= 26; i++)
      if (h[i] > 0 && ('a' + i - 1 >= str2[l])) {
        find = true;
        ist(ans, (char)('a' + i - 1));
        l++;
        stt = 1;
        break;
      }
    if (big(ans, str2)) break;
    if (l == len && !big(ans, str2)) find = false;
    while (!find) {
      if (l == 0) return false;
      l--;
      del(ans);
      for (i = 1; i <= 26; i++)
        if (h[i] > 0 && 'a' + i - 1 > str2[l]) {
          find = true;
          ist(ans, (char)('a' + i - 1));
          l++;
          bigout = true;
          break;
        }
    }
    if (bigout) break;
  }
  for (; l < len;) {
    for (i = 1; i <= 26; i++)
      if (h[i] > 0) {
        l++;
        ist(ans, (char)('a' + i - 1));
        break;
      }
  }
  cout << ans << endl;
  return true;
}
void del(char* str) {
  int len;
  len = strlen(str);
  if (len == 0) return;
  h[str[len - 1] - 'a' + 1]++;
  str[len - 1] = '\0';
}
void ist(char* str, char x) {
  int len;
  len = strlen(str);
  str[len] = x;
  str[len + 1] = '\0';
  h[x - 'a' + 1]--;
}
