#include <bits/stdc++.h>
using namespace std;
const int iINF = 0x3f3f3f3f;
const long long lINF = 0x3f3f3f3f3f3f3f3f;
inline void read(long long& x) {
  int f = 1;
  char c;
  while (((c = getchar()) < '0' || c > '9') && c != '-')
    ;
  c == '-' ? (f = -1, x = 0) : (x = c - '0');
  while ((c = getchar()) >= '0' && c <= '9') x = x * 10 + c - '0';
  x *= f;
}
inline void read(int& x) {
  long long t;
  read(t);
  x = t;
}
char S[6000], T[6000];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> T >> S;
  int N = strlen(S), M = strlen(T);
  for (int i = 0; i < M; ++i) {
    for (int j = i; j < M; ++j)
      if (T[j] == S[i]) {
        swap(T[i], T[j]);
        break;
      }
    if (T[i] == S[i]) {
      sort(T + i + 1, T + M, greater<char>());
      if (string(T) > string(S)) continue;
    }
    int pick = -1;
    for (int j = i; j < M; ++j)
      if (T[j] > S[i] && (pick == -1 || T[j] < T[pick])) pick = j;
    if (pick == -1) {
      printf("-1\n");
      return 0;
    }
    swap(T[i], T[pick]);
    sort(T + i + 1, T + M);
    break;
  }
  cout << T << endl;
  return 0;
}
