#include <bits/stdc++.h>
using namespace std;
const long double E = 2.71828182845904523536;
const long double pi = acos(-1);
const double eps = 1e-9;
const long long mod = 1e9 + 7;
const long long inf = 1LL << 30;
const int N = 5050;
char s1[N], s2[N];
multiset<char> s;
int main() {
  scanf("%s", &s1);
  scanf("%s", &s2);
  int n1 = strlen(s1);
  int n2 = strlen(s2);
  for (int i = 0; i < n1; i++) {
    s.insert(s1[i]);
  }
  int pos = n2;
  vector<int> v;
  for (int i = 0; i < n2; i++) {
    if (s.find(s2[i]) == s.end()) {
      pos = i;
      break;
    }
    s.erase(s.find(s2[i]));
    v.push_back(s2[i]);
  }
  for (auto c : v) s.insert(c);
  if (!pos) {
    if (s.upper_bound(s2[0]) == s.end()) {
      printf("-1");
      return 0;
    }
    string ans = "";
    char c = *s.upper_bound(s2[0]);
    ans += c;
    s.erase(s.find(c));
    for (auto x : s) ans += x;
    printf("%s", ans.c_str());
    return 0;
  }
  if (pos == n2 && n1 > n2) {
    string ans = "";
    for (int i = 0; i < n2; i++) {
      ans += s2[i];
      s.erase(s.find(s2[i]));
    }
    for (auto c : s) ans += c;
    printf("%s", ans.c_str());
    return 0;
  }
  v.clear();
  int pos1 = 0;
  for (int i = 0; i < pos; i++) {
    if (s.upper_bound(s2[i]) != s.end()) pos1 = i;
    s.erase(s.find(s2[i]));
    v.push_back(s2[i]);
  }
  if (s.upper_bound(s2[pos]) != s.end()) pos1 = pos;
  for (auto c : v) s.insert(c);
  if (s.upper_bound(s2[pos1]) == s.end()) {
    printf("-1");
    return 0;
  }
  string ans = "";
  for (int i = 0; i < pos1; i++) {
    ans += s2[i];
    s.erase(s.find(s2[i]));
  }
  char c = *s.upper_bound(s2[pos1]);
  ans += c;
  s.erase(s.find(c));
  for (auto x : s) ans += x;
  printf("%s", ans.c_str());
  return 0;
}
