#include <bits/stdc++.h>
using namespace std;
string a, b;
vector<array<int,10>> nextA, nextB;
vector<vector<int>> memoLen;
// build next-pos arrays
vector<array<int,10>> buildNext(const string &s) {
int n = s.size();
vector<array<int,10>> nxt(n+1);
for (int d = 0; d < 10; ++d) nxt[n][d] = -1;
for (int i = n-1; i >= 0; --i) {
nxt[i] = nxt[i+1];
nxt[i][s[i]-'0'] = i;
}
return nxt;
}
// memoized length of best subsequence from (i,j)
int lcsLen(int i, int j) {
if (i >= (int)a.size() || j >= (int)b.size()) return 0;
int &res = memoLen[i][j];
if (res != -1) return res;
res = 0;
for (int d = 0; d <= 9; ++d) {
int ni = nextA[i][d];
int nj = nextB[j][d];
if (ni != -1 && nj != -1) {
res = max(res, 1 + lcsLen(ni+1, nj+1));
}
}
return res;
}
// build lexicographically largest string of length = lcsLen(i,j)
string buildSuffix(int i, int j) {
if (i >= (int)a.size() || j >= (int)b.size()) return "";
int need = lcsLen(i, j);
if (need == 0) return "";
for (int d = 9; d >= 0; --d) {
int ni = nextA[i][d];
int nj = nextB[j][d];
if (ni != -1 && nj != -1) {
if (1 + lcsLen(ni+1, nj+1) == need) {
return string(1, char('0'+d)) + buildSuffix(ni+1, nj+1);
}
}
}
return "";
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
if (!(cin >> a >> b)) return 0;
nextA = buildNext(a);
nextB = buildNext(b);
memoLen.assign(a.size()+1, vector<int>(b.size()+1, -1));
// fill memo lazily via calls below
// Now choose best starting non-zero digit to maximize numeric value
string best = "";
int bestLen = -1; // length of trimmed result (digits after first non-zero, including it)
for (int d = 9; d >= 1; --d) {
int ni = nextA[0][d];
int nj = nextB[0][d];
if (ni != -1 && nj != -1) {
int len = 1 + lcsLen(ni+1, nj+1); // trimmed length if start with d
string cand = string(1, char('0'+d)) + buildSuffix(ni+1, nj+1);
if (len > bestLen || (len == bestLen && cand > best)) {
bestLen = len;
best = move(cand);
}
}
}
if (bestLen == -1) {
// no non-zero start possible; check if we can output "0"
if (nextA[0][0] != -1 && nextB[0][0] != -1) {
cout << "0\n";
} else {
// no common subsequence at all
cout << "0\n"; // hoặc "" tùy spec; ở đây in 0 cho an toàn
}
} else {
cout << best << "\n";
}
return 0;
}