#include <bits/stdc++.h>
using namespace std;
bool compare(pair<int, int> pair1, pair<int, int> pair2) {
  return (pair1.first < pair2.first ||
          (pair1.first == pair2.first && pair1.second < pair2.second));
}
vector<long long> v;
vector<long long> m;
vector<pair<int, int> > r;
int main() {
  ios::sync_with_stdio(false);
  string s, t;
  cin >> s >> t;
  sort(s.begin(), s.end());
  string c = s;
  reverse(c.begin(), c.end());
  if (t >= c) {
    cout << -1;
    return 0;
  }
  for (int i = 0; i < min(s.size(), t.size()); i++) {
    int j = distance(s.begin(), lower_bound(s.begin() + i, s.end(), t[i]));
    if (j >= s.size()) {
      sort(s.begin() + i, s.end());
      reverse(s.begin() + i, s.end());
      next_permutation(s.begin(), s.end());
      break;
    }
    swap(s[i], s[j]);
    sort(s.begin() + i + 1, s.end());
    if (s[i] > t[i]) {
      break;
    }
  }
  if (s == t) {
    next_permutation(s.begin(), s.end());
  }
  cout << s << endl;
  return 0;
}
