#include <bits/stdc++.h>
using namespace std;
const int MaxN = 5005;
char s[MaxN], t[MaxN], ans[MaxN], at[MaxN];
int cnt[30], c[30], n, m;
int check(int pos) {
  for (int i = 0; i < 26; i++) c[i] = cnt[i];
  memcpy(at, ans, sizeof(ans));
  int j = 25;
  for (int i = pos; i < n; i++) {
    while (!cnt[j]) j--;
    cnt[j]--;
    at[i] = j + 'a';
  }
  for (int i = 0; i < 26; i++) cnt[i] = c[i];
  if (strcmp(at, t) > 0) return 1;
  return 0;
}
int main() {
  scanf("%s%s", s, t);
  n = strlen(s);
  m = strlen(t);
  for (int i = 0; i < n; i++) {
    cnt[s[i] - 'a']++;
  }
  int i = 0;
  for (; i < m; i++) {
    if (cnt[t[i] - 'a']) {
      cnt[t[i] - 'a']--;
      ans[i] = t[i];
      if (check(i + 1)) {
        continue;
      }
      cnt[t[i] - 'a']++;
    }
    int ok = 0;
    for (int j = t[i] - 'a' + 1; j < 26; j++) {
      if (cnt[j]) {
        cnt[j]--;
        ans[i] = j + 'a';
        ok = 1;
        break;
      }
    }
    if (!ok) {
      puts("-1");
      return 0;
    } else
      break;
  }
  if (i < m) i++;
  for (; i < n; i++) {
    for (int j = 0; j < 26; j++)
      if (cnt[j]) {
        cnt[j]--;
        ans[i] = j + 'a';
        break;
      }
  }
  printf("%s\n", ans);
  return 0;
}
