#include <bits/stdc++.h>
#define C make_pair
#define ll long long
#define all(a) a.begin(),a.end()
#define name "task"
#define ln "\n"
using namespace std;
int n,k;
map<int,int> pos,vt;
const int maxN=5*1e5+9;
int a[maxN],b[maxN];
vector<int> ans;
int len=1;
void inp(){
cin>>n>>k;
}
void solve_if_k_equal_1(){
for(int i=0;i<n;++i){
cin>>a[i];
pos[a[i]]=i;
}
for(int i=0;i<n;++i){
cin>>b[i];
vt[i]=pos[b[i]];
}
for(int i=0;i<n;++i){
auto it=lower_bound(ans.begin(),ans.end(),vt[i]);
if(it!=ans.end()) *it=vt[i];
else ans.push_back(vt[i]);
}
cout<<ans.size();
}
void solve_if_n_less_than_1e3(){
n*=k;
for(int i=0;i<n;++i)
cin>>a[i];
for(int i=0;i<n;++i)
cin>>b[i];
ll dp[n+9][n+9];
memset(dp,0,sizeof(dp));
for(int i=1;i<=n;++i){
for(int j=1;j<=n;++j){
if(a[i-1]==b[j-1])
dp[i][j]=dp[i-1][j-1]+1;
else{
dp[i][j]=max(dp[i-1][j],dp[i][j-1]);
}
}
}
cout<<dp[n][n];
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if(fopen(name".inp","r")){
freopen(name".inp","r",stdin);
freopen(name".out","w",stdout);
}
inp();
if(k==1){
solve_if_k_equal_1();
} else{
solve_if_n_less_than_1e3();
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CiNkZWZpbmUgQyBtYWtlX3BhaXIKI2RlZmluZSBsbCBsb25nIGxvbmcKI2RlZmluZSBhbGwoYSkgYS5iZWdpbigpLGEuZW5kKCkKI2RlZmluZSBuYW1lICJ0YXNrIgojZGVmaW5lIGxuICJcbiIKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKCmludCBuLGs7Cm1hcDxpbnQsaW50PiBwb3MsdnQ7CmNvbnN0IGludCBtYXhOPTUqMWU1Kzk7CmludCBhW21heE5dLGJbbWF4Tl07CnZlY3RvcjxpbnQ+IGFuczsKaW50IGx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