#include <bits/stdc++.h>
using namespace std;
int cnts[26];
int great(int k) {
  for (int i = k + 1; i < 26; i++) {
    if (cnts[i]) return i;
  }
  return -1;
}
void fill(string &s, int n, int pos) {
  int aux[26];
  for (int i = 0; i < 26; i++) aux[i] = cnts[i];
  for (int i = 0; i < 26 && pos <= n; i++) {
    while (aux[i] && pos <= n) {
      aux[i]--;
      s[pos++] = (char)i + 'a';
    }
  }
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  string s, t, res;
  int n, m, aux, pos = 0, last = -1, j;
  cin >> s >> t;
  n = s.length(), m = t.length();
  for (int i = 0; i < 26; i++) cnts[i] = 0;
  res.assign(n, ' ');
  for (int i = 0; i < n; i++) {
    aux = s[i] % 'a';
    cnts[aux]++;
  }
  bool flag = false;
  while (pos < min(n, m)) {
    aux = t[pos] % 'a';
    if (cnts[aux]) {
      last = pos;
      cnts[aux]--;
      res[pos] = (char)aux + 'a';
    } else {
      j = great(aux);
      if (j != -1) {
        cnts[j]--;
        res[pos] = (char)j + 'a';
        flag = true;
        pos++;
        break;
      } else {
        pos++;
        break;
      }
    }
    pos++;
  }
  if (!flag) {
    while (last != -1) {
      fill(res, n, last + 1);
      if (t.compare(res) < 0) {
        cout << res << '\n';
        break;
      }
      aux = res[last] % 'a';
      cnts[aux]++;
      j = great(aux);
      if (j != -1) {
        cnts[j]--;
        res[last] = (char)j + 'a';
      }
      fill(res, n, last + 1);
      if (t.compare(res) < 0) {
        cout << res << '\n';
        break;
      }
      last--;
    }
    if (last == -1) cout << "-1\n";
  } else {
    fill(res, n, pos);
    cout << res << '\n';
  }
  return 0;
}
