#include <bits/stdc++.h>
using namespace std;
void swap(char* a, char* b) {
  if (*a == *b) return;
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
}
void rev(string& s, int l, int r) {
  while (l < r) swap(&s[l++], &s[r--]);
}
int bsearch(string& s, int l, int r, int key) {
  int index = -1;
  while (l <= r) {
    int mid = l + (r - l) / 2;
    if (s[mid] <= key)
      r = mid - 1;
    else {
      l = mid + 1;
      if (index = -1 || s[index] <= s[mid]) index = mid;
    }
  }
  return index;
}
bool nextpermutation(string& s) {
  int len = s.length(), i = len - 2;
  while (i >= 0 && s[i] >= s[i + 1]) --i;
  if (i < 0)
    return false;
  else {
    int index = bsearch(s, i + 1, len - 1, s[i]);
    swap(&s[i], &s[index]);
    rev(s, i + 1, len - 1);
    return true;
  }
}
string return_lowest(string input) {
  sort(input.begin(), input.end());
  return input;
}
string find_smallest(string input, string allowed) {
  if ((input).length() == 0)
    if (allowed.length() == 0)
      return "";
    else
      return return_lowest(allowed);
  size_t found = allowed.find(input.substr(0, 1));
  if (found == string::npos) {
    int x = int(input.c_str()[0]);
    for (char i = x; i <= 'z'; i++) {
      size_t found = allowed.find(i);
      if (found != string::npos) {
        string temp = allowed;
        string allowed2 = allowed.erase(found, 1);
        return (temp[found] + return_lowest(allowed2));
      }
    }
    return "falseFalse";
  }
  {
    int x = int(input.c_str()[0]);
    for (char i = x; i <= 'z'; i++) {
      string allowed2 = allowed;
      size_t found = allowed2.find(i);
      if (found != string::npos) {
        allowed2.erase(found, 1);
        string fuc;
        if (i == x)
          fuc = find_smallest(input.substr(1, (input).length() - 1), allowed2);
        else {
          fuc = return_lowest(allowed2);
        }
        if (fuc == "falseFalse") continue;
        return allowed[found] + fuc;
      }
    }
    return "falseFalse";
  }
}
int main() {
  string input, allowed;
  cin >> allowed >> input;
  sort(allowed.begin(), allowed.end());
  string temp = find_smallest(input, allowed);
  if (temp == "falseFalse")
    cout << -1;
  else if (temp == input) {
    nextpermutation(temp);
    if (temp == input)
      cout << -1;
    else
      cout << temp;
  } else
    cout << temp;
}
