#include <bits/stdc++.h>
using namespace std;
string a, b, c, s;
int main() {
  while (cin >> a >> b) {
    sort(a.begin(), a.end());
    int i = 0, r = -1, k = -1;
    bool f = 0;
    c = "";
    while (i < b.length() && !a.empty()) {
      int g = upper_bound(a.begin(), a.end(), b[i]) - a.begin();
      if (a.length() - g > 0) {
        k = c.length();
        r = g;
        s = a;
      }
      --g;
      if (f < 0 || a[g] < b[i]) {
        ++g;
        f = 1;
      }
      if (g < 0 || g >= a.length()) break;
      c += a[g];
      a.erase(a.begin() + g);
      if (f) break;
      ++i;
    }
    c += a;
    if (c > b)
      cout << c << endl;
    else {
      if (r != -1) {
        c = c.substr(0, k);
        c += s[r];
        s.erase(s.begin() + r);
        c += s;
        cout << c << endl;
      } else {
        cout << "-1" << endl;
      }
    }
  }
}
