#include <bits/stdc++.h>
using namespace std;
inline int ni() {
  int a;
  scanf("%d", &a);
  return a;
}
const int max_n = 5000;
char s1[max_n + 1], s2[max_n + 1], res[max_n + 1];
int digits[26], minuses[26];
bool check(int from, int code, int n, int m) {
  bool ok = true, founded = false;
  digits[code]--;
  memset(minuses, 0, sizeof(minuses));
  for (int i = from; i < m; i++) {
    int next = -1, need = s2[i] - 'a';
    for (int j = 25; j >= 0; j--) {
      if (j >= need && digits[j] > 0) {
        next = j;
        break;
      }
    }
    if (next == -1) {
      ok = false;
      break;
    } else if (next > need) {
      founded = true;
      break;
    } else {
      digits[next]--;
      minuses[next]++;
    }
  }
  for (int i = 0; i < int(26); i++) digits[i] += minuses[i];
  digits[code]++;
  if (n <= m && !founded) ok = false;
  return ok;
}
int main() {
  scanf("%s", s1);
  scanf("%s", s2);
  int n = strlen(s1), m = strlen(s2);
  bool found = true, before = false;
  for (int i = 0; i < int(n); i++) digits[s1[i] - 'a']++;
  for (int i = 0; i < int(n); i++) {
    if (i >= m || before) {
      int code = -1;
      for (int j = 0; j < int(26); j++) {
        if (digits[j] > 0) {
          code = j;
          break;
        }
      }
      res[i] = 'a' + code;
      digits[code]--;
    } else {
      int same = s2[i] - 'a', next = -1;
      for (int j = 0; j < int(26); j++) {
        if (j > same && digits[j] > 0) {
          next = j;
          break;
        }
      }
      if (digits[same] > 0 && check(i + 1, same, n, m)) {
        digits[same]--;
        res[i] = 'a' + same;
      } else if (next != -1) {
        digits[next]--;
        res[i] = 'a' + next;
        before = true;
      } else {
        found = false;
        break;
      }
    }
  }
  if (found) {
    res[n] = '\0';
    printf("%s\n", res);
  } else {
    printf("-1\n");
  }
  return 0;
}
