#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 3e5 + 5;
mt19937 rnd(51515);
int main() {
  ios ::sync_with_stdio(0);
  cin.tie(0);
  string s, t;
  cin >> s >> t;
  sort(s.begin(), s.end());
  reverse(s.begin(), s.end());
  if (s <= t) {
    cout << -1;
    return 0;
  }
  map<char, int> mp;
  for (auto c : s) {
    ++mp[c];
  }
  string new_str;
  for (int i = 0; i < t.size(); ++i) {
    map<char, int> mt = mp;
    if (mt[t[i]]) {
      bool ok = 0;
      int j = i + 1;
      mt[t[i]]--;
      if (j == t.size()) {
        bool good = 0;
        for (char h = 'z'; h >= 'a'; h--) {
          good |= (mt[h] != 0);
        }
        ok = good;
      }
      while (j < t.size()) {
        bool horosho = 0;
        for (char c = 'z'; c >= t[j]; c--) {
          if (mt[c]) horosho = 1;
          if (c > t[j] && mt[c]) {
            ok = 1;
            break;
          } else if (c == t[j] && mt[c]) {
            mt[c]--;
            ++j;
            if (j == t.size()) {
              bool good = 0;
              for (char h = 'z'; h >= 'a'; h--) {
                good |= (mt[h] != 0);
              }
              ok = good;
            }
            break;
          }
        }
        if (ok || !horosho) break;
      }
      if (ok) {
        new_str += t[i];
        mp[t[i]]--;
        continue;
      }
    }
    for (char c = t[i] + 1; c <= 'z'; ++c) {
      if (mp[c]) {
        new_str += c;
        mp[c]--;
        for (char c = 'a'; c <= 'z'; ++c) {
          while (mp[c]) {
            new_str += c;
            mp[c]--;
          }
        }
        break;
      }
    }
  }
  for (char c = 'a'; c <= 'z'; ++c) {
    while (mp[c]) {
      new_str += c;
      mp[c]--;
    }
  }
  cout << new_str;
  return 0;
}
