#include <iostream>
#define FIN "cmlsc.in"
#define FOUT "cmlsc.out"
#define DIM 1024
#define max(a,b) ((a > b) ? a : b)
#define FOR(i,a,b) for(i=a;i<=b;++i)
typedef unsigned int u;
u X[DIM], Y[DIM], Answer[DIM], LCS[DIM][DIM], n, m, i, j, k;
int main(int argc, char const *argv[])
{
//freopen(FIN, "r", stdin);
//freopen(FOUT, "w", stdout);
std::cin>>n>>m;
FOR(i, 1, n) std::cin>>X[i];
FOR(j, 1, m) std::cin>>Y[j];
FOR(i,1,n)
FOR(j,1,m)
if(X[i] == Y[j]) LCS[i][j] = 1 + LCS[i-1][j-1];
else
LCS[i][j] = max(LCS[i][j-1], LCS[i-1][j]);
std::cout<<LCS[n][m]<<"\n";
i = n; j = m;
while(i!=0 && j!=0) {
if(X[i] == Y[j]) {
Answer[++k] = X[i];
i--;j--;
} else {
if(LCS[i][j-1] < LCS[i-1][j]) i--;
else j--;
}
}
for(i = k; i != 0; i--) std::cout<<Answer[i]<<" ";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojZGVmaW5lIEZJTiAiY21sc2MuaW4iCiNkZWZpbmUgRk9VVCAiY21sc2Mub3V0IgojZGVmaW5lIERJTSAxMDI0CiNkZWZpbmUgbWF4KGEsYikgKChhID4gYikgPyBhIDogYikKI2RlZmluZSBGT1IoaSxhLGIpIGZvcihpPWE7aTw9YjsrK2kpCgp0eXBlZGVmIHVuc2lnbmVkIGludCB1OwoKdSBYW0RJTV0sIFlbRElNXSwgQW5zd2VyW0RJTV0sIExDU1tESU1dW0RJTV0sIG4sIG0sIGksIGosIGs7CgppbnQgbWFpbihpbnQgYXJnYywgY2hhciBjb25zdCAqYXJndltdKQp7CgkvL2ZyZW9wZW4oRklOLCAiciIsIHN0ZGluKTsKCQkvL2ZyZW9wZW4oRk9VVCwgInciLCBzdGRvdXQpOwoKCXN0ZDo6Y2luPj5uPj5tOwoJCiAgICBGT1IoaSwgMSwgbikgc3RkOjpjaW4+PlhbaV07CiAgICA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