#include <bits/stdc++.h>
inline int Max(int a, int b) { return a > b ? a : b; }
inline int Min(int a, int b) { return a < b ? a : b; }
inline int gcd(int a, int b) { return a % b == 0 ? b : gcd(b, a % b); }
inline int bit(int x) { return (int)log10((double)x) + 1; }
inline int lowbit(int x) { return x & -x; }
inline int highbit(int x) {
  while (x != lowbit(x)) x ^= lowbit(x);
  return x;
}
inline int whichbit(int x) {
  int ret = 0;
  while (x > 1) {
    x >>= 1;
    ret++;
  }
  return ret;
}
using namespace std;
const double pi = M_PI;
const int inf = 1000000007;
const int mod = 10000;
const double eps = 1e-6;
const int maxn = (1 << 15) + 5;
const int maxm = 1005;
const int maxe = maxn * 4;
char s[5005];
char x[5005];
int tot[300];
char ans[5005];
int now;
int main() {
  now = 0;
  gets(s);
  gets(x);
  memset(tot, 0, sizeof(tot));
  for (int i = 0; s[i]; i++) tot[s[i]]++;
  int get = 0;
  for (int i = 0; x[i]; i++) {
    if (tot[x[i]] == 0) {
      int flag = 0;
      for (int j = x[i] + 1; j <= 'z'; j++) {
        if (tot[j] != 0) {
          flag = j;
          break;
        }
      }
      if (flag) {
        ans[now++] = flag;
        tot[flag]--;
        for (int j = 'a'; j <= 'z'; j++) {
          while (tot[j] > 0) {
            ans[now++] = j;
            tot[j]--;
          }
        }
        ans[now] = 0;
        break;
      } else {
        now--;
        while (now >= 0) {
          tot[ans[now]]++;
          int flag = 0;
          for (int j = ans[now] + 1; j <= 'z'; j++) {
            if (tot[j] > 0) {
              flag = j;
              break;
            }
          }
          if (flag) {
            ans[now++] = flag;
            tot[flag]--;
            for (int j = 'a'; j <= 'z'; j++) {
              while (tot[j] > 0) {
                ans[now++] = j;
                tot[j]--;
              }
            }
            ans[now] = 0;
            break;
          } else {
            now--;
          }
        }
        if (now < 0) {
          get = 1;
          break;
        }
        break;
      }
    } else {
      ans[now++] = x[i];
      tot[x[i]]--;
    }
  }
  if (get) {
    puts("-1");
  } else {
    for (int j = 'a'; j <= 'z'; j++) {
      while (tot[j] > 0) {
        ans[now++] = j;
        tot[j]--;
      }
    }
    if (strcmp(ans, x) <= 0) {
      now--;
      while (now >= 0) {
        tot[ans[now]]++;
        int flag = 0;
        for (int j = ans[now] + 1; j <= 'z'; j++) {
          if (tot[j] > 0) {
            flag = j;
            break;
          }
        }
        if (flag) {
          ans[now++] = flag;
          tot[flag]--;
          for (int j = 'a'; j <= 'z'; j++) {
            while (tot[j] > 0) {
              ans[now++] = j;
              tot[j]--;
            }
          }
          ans[now] = 0;
          break;
        } else {
          now--;
        }
      }
      if (now < 0) {
        get = 1;
      }
    }
    if (get) {
      puts("-1");
    } else {
      ans[now] = 0;
      puts(ans);
    }
  }
  return 0;
}
