Program MaxCommonSubnumber;
Uses Math;

Const MaxLength = 3000;

Var f: Text;
    n1, n2: Array [1..MaxLength] of Char;  //input strings
    len1, len2: Word;

    r: Array [1..MaxLength+1, 1..MaxLength+1] of Word;  //main table

    p1, p2: Array ['0'..'9'] of Word;
    lenA, i1, i2, w, j1, j2: Word;
    c, ci: Char;

Begin
//  Assign(f, 'common.in'); Reset(f);
  len1:= 0;
  While not SeekEoLn Do Begin
    Inc(len1);
    Read(n1[len1]);
  End;
  Readln();
  len2:= 0;
  While not SeekEoLn Do Begin
    Inc(len2);
    Read(n2[len2]);
  End;
//  Close(f);

  {Fill the table}
  For w:= 1 To len1+1 Do Begin
    r[w,len2+1]:= 0;
  End;
  For w:= 1 To len2 Do Begin
    r[len1+1,w]:= 0;
  End;

  For i1:= len1 DownTo 1 Do Begin
    For i2:= len2 DownTo 1 Do Begin
      w:= max ( r[i1+1,i2], r[i1,i2+1] );
      If n1[i1] = n2[i2] Then Begin
        w:= max (w, r[i1+1,i2+1] + 1);
      End;
      r[i1, i2]:= w;
    End;
  End;

  {Output}
//  Assign(f, 'common.out'); Rewrite(f);

  {Find first digit and length of the answer}

  For ci:= '0' To '9' Do Begin
    p1[ci]:= 1;
    While (p1[ci]<=len1) and (n1[p1[ci]]<>ci) Do Inc(p1[ci]);
    p2[ci]:= 1;
    While (p2[ci]<=len2) and (n2[p2[ci]]<>ci) Do Inc(p2[ci]);
  End;
  lenA:= r[p1['9'], p2['9']];
  c:= '9';
  For ci:= '8' DownTo '1' Do Begin
    If r[p1[ci], p2[ci]]>lenA Then Begin
      lenA:= r[p1[ci], p2[ci]];
      c:= ci;
    End;
  End;
  If lenA=0 Then Begin
    c:= '0';
    lenA:= 1;
  End;
  Write(c);
  Dec(lenA);
  {Find others}
  While lenA>0 Do Begin
    j1:= p1[c]+1;
    j2:= p2[c]+1;
    For ci:= '0' To '9' Do Begin
      If p1[ci]< j1 Then Begin
        p1[ci]:= j1;
        While (p1[ci]<=len1) and (n1[p1[ci]]<>ci) Do Inc(p1[ci]);
      End;
      If p2[ci]< j2 Then Begin
        p2[ci]:= j2;
        While (p2[ci]<=len2) and (n2[p2[ci]]<>ci) Do Inc(p2[ci]);
      End;
    End;
    c:= '9';
    While r[p1[c], p2[c]]<lenA Do Begin
      Dec(c);
    End;
    Write(c);
    Dec(lenA);
  End;
  Writeln();
//  Close(f);
End.
