#include <bits/stdc++.h>
using namespace std;
const int MAXN = 5010;
struct node {
  int a[30];
  node() { memset(a, 0, sizeof(a)); }
  friend bool operator<=(const node &x, const node &y) {
    int lenx = 0, leny = 0;
    for (int i = 26; i >= 0; i--) lenx += x.a[i];
    for (int i = 26; i >= 0; i--) leny += y.a[i];
    for (int i = 26; i >= 0; i--) {
      if (x.a[i] != y.a[i]) {
        if (!(lenx == x.a[i] && leny == y.a[i]))
          return x.a[i] < y.a[i];
        else {
          if (lenx != leny) return lenx < leny;
          return true;
        }
      }
      lenx -= x.a[i];
      leny -= y.a[i];
    }
    return true;
  }
};
node dp[MAXN];
char s[MAXN], t[MAXN];
void outit(node x) {
  for (int j = 26; j >= 0; j--)
    for (int k = 0; k < x.a[j]; k++) cout << char(j + 'a');
  cout << endl;
}
int main() {
  while (scanf("%s%s", s, t) == 2) {
    int mx = 0;
    int n = strlen(t);
    for (int i = n - 1; i >= 0; i--) {
      int Mx = 26;
      for (int j = 25; j >= 0; j--) {
        if (dp[i + 1].a[j] > 0) {
          Mx = j;
          break;
        }
      }
      int te = t[i] - 'a';
      if (te >= Mx) {
        for (int j = 0; j <= 26; j++) dp[i].a[j] = dp[i + 1].a[j];
        dp[i].a[te]++;
      } else {
        memset(dp[i].a, 0, sizeof(dp[i].a));
        dp[i].a[te + 1] = 1;
      }
    }
    node tmp;
    memset(tmp.a, 0, sizeof(tmp.a));
    for (int i = 0; i < n; i++) tmp.a[t[i] - 'a']++;
    int m = strlen(s);
    node cur;
    memset(cur.a, 0, sizeof(cur.a));
    for (int i = 0; i < m; i++) cur.a[s[i] - 'a']++;
    bool fuck = true;
    for (int i = 0; i < 26; i++)
      if (cur.a[i] < tmp.a[i]) fuck = false;
    if (fuck && (tmp <= cur && (!(cur <= tmp)))) {
      printf("%s", t);
      for (int i = 0; i < 26; i++) {
        cur.a[i] -= tmp.a[i];
        for (int j = 0; j < cur.a[i]; j++) printf("%c", i + 'a');
      }
      continue;
    }
    int ac = -1;
    bool flag = false;
    while (ac + 1 <= n - 1 && dp[ac + 1] <= cur) {
      ac++;
      node te = cur;
      int find = -1;
      for (int j = 25; j >= t[ac] - 'a'; j--) {
        if (te.a[j] > 0) {
          te.a[j]--;
          if (dp[ac + 1] <= te || j > t[ac] - 'a') find = j;
          te.a[j]++;
        }
      }
      if (find > t[ac] - 'a') {
        for (int j = 0; j < ac; j++) printf("%c", t[j]);
        printf("%c", find + 'a');
        cur.a[find]--;
        for (int j = 0; j < 26; j++)
          for (int k = 0; k < cur.a[j]; k++) printf("%c", j + 'a');
        flag = true;
        cout << endl;
        break;
      } else
        cur.a[find]--;
    }
    if (flag) continue;
    if (ac == -1) {
      printf("-1\n");
      continue;
    }
    printf("%s", t);
    for (int j = 0; j < 26; j++)
      for (int k = 0; k < cur.a[j]; k++) printf("%c", j + 'a');
  }
  return 0;
}
