#include <bits/stdc++.h>
template <class T>
T gcd(T a, T b) {
  T r;
  while (b != 0) {
    r = a % b;
    a = b;
    b = r;
  }
  return a;
}
template <class T>
T lcm(T a, T b) {
  return a / gcd(a, b) * b;
}
template <class T>
T sqr(T x) {
  return x * x;
}
template <class T>
T cube(T x) {
  return x * x * x;
}
using namespace std;
string s;
string a;
vector<int> aa(20);
vector<int> bb(20);
string stringget(int n, char x) {
  string ans = "";
  for (int i = 1; i <= n; i++) ans += x;
  return ans;
}
string take(vector<int> cnt) {
  string ans = "";
  for (int i = 0; i < 10; i++) ans += stringget(cnt[i], char(i + '0'));
  return ans;
}
int main() {
  cin >> s;
  cin >> a;
  for (int i = 0; i < s.length(); i++) aa[s[i] - '0']++;
  for (int i = 0; i < a.length(); i++) bb[a[i] - '0']++;
  for (int length = 1; length < s.length(); length++) {
    if (length >= s.length()) {
      cout << -1;
      break;
    }
    vector<int> cnt(20);
    cnt = aa;
    for (int i = 0; i < 10; i++) cnt[i] = aa[i];
    int Length = 0;
    int tmp = length;
    while (tmp > 0) {
      Length++;
      cnt[tmp % 10]--;
      tmp /= 10;
    }
    bool canDo = true;
    bool have = false;
    int minVal = 10;
    for (int i = 0; i < 10; i++) {
      cnt[i] -= bb[i];
      if (cnt[i] < 0) canDo = false;
      if (cnt[i] > 0 && i > 0) {
        have = true;
        minVal = min(minVal, i);
      }
    }
    if (!canDo || (!have && a[0] == '0') || s.length() - Length != length)
      continue;
    string Ans = "";
    bool Have = false;
    if (a[0] != '0') {
      Ans = a + take(cnt);
      Have = true;
    }
    string Pre = "";
    Pre += char(minVal + '0');
    cnt[minVal]--;
    for (int i = 0; i < 10; i++) {
      string tmp = Pre + a + take(cnt);
      if (!Have || Ans > tmp)
        if (tmp[0] != '0') {
          Ans = tmp;
          Have = true;
        }
      Pre = Pre + stringget(cnt[i], i + '0');
      cnt[i] = 0;
    }
    cout << Ans;
    return 0;
  }
  cout << 0;
}
