#include <iostream>
#include <cstdio>
using namespace std;
const int NMAX = 1004;
const int KMAX = 6;
int a[KMAX][NMAX], pos[KMAX][NMAX] , dp[NMAX];
int main(){
#ifndef ONLINE_JUDGE
freopen("date.in","r",stdin);
freopen("date.out","w",stdout);
#endif
int n, k,sol = 0;
cin >> n >> k;
for(int i = 1;i <= k; ++i)
for(int j = 1;j <= n; ++j)
{
cin >> a[i][j];
pos[i][a[i][j]] = j;
}
for(int j = 1;j <= n; ++j)
{
int maxx = 0;
for(int p = 1;p < j; ++p){
int i;
for(i = 2;i <= k && pos[i][a[1][p]] < pos[i][a[1][j]];++i);
if(i==k+1 && dp[p] > maxx)
maxx = dp[p];
}
dp[j] = maxx+1;
sol = max(sol,dp[j]);
}
cout<<sol<<"\n";
return 0;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8Y3N0ZGlvPgoKdXNpbmcgbmFtZXNwYWNlIHN0ZDsKY29uc3QgaW50IE5NQVggPSAxMDA0Owpjb25zdCBpbnQgS01BWCA9IDY7CmludCBhW0tNQVhdW05NQVhdLCBwb3NbS01BWF1bTk1BWF0gLCBkcFtOTUFYXTsKaW50IG1haW4oKXsKICAgICNpZm5kZWYgT05MSU5FX0pVREdFCiAgICAgICAgZnJlb3BlbigiZGF0ZS5pbiIsInIiLHN0ZGluKTsKICAgICAgICBmcmVvcGVuKCJkYXRlLm91dCIsInciLHN0ZG91dCk7CiAgICAjZW5kaWYKICAgIGludCBuLCBrLHNvbCA9IDA7CiAgICBjaW4gPj4gbiA+PiBrOwogICAgZm9yKGludCBpID0gMTtpIDw9IGs7ICsraSkKICAgICAgICBmb3IoaW50IGogPSAxO2ogPD0gbjsgKytqKQogICAgICAgIHsKICAgICAgICAgICAgY2luID4+IGFbaV1bal07CiAgICAgICAgICAgIHBvc1tpXVthW2ldW2pdXSA9IGo7CiAgICAgICAgfQogICAgZm9yKGludCBqID0gMTtqIDw9IG47ICsraikKICAgIHsKICAgICAgICBpbnQgbWF4eCA9IDA7CiAgICAgICAgZm9yKGludCBwID0gMTtwIDwgajsgKytwKXsKICAgICAgICAgICAgaW50IGk7CiAgICAgICAgICAgIGZvcihpID0gMjtpIDw9IGsgJiYgcG9zW2ldW2FbMV1bcF1dIDwgcG9zW2ldW2FbMV1bal1dOysraSk7CiAgICAgICAgICAgIGlmKGk9PWsrMSAmJiBkcFtwXSA+IG1heHgpCiAgICAgICAgICAgICAgICBtYXh4ID0gZHBbcF07CiAgICAgICAgfQogICAgICAgIGRwW2pdID0gbWF4eCsxOwogICAgICAgIHNvbCA9IG1heChzb2wsZHBbal0pOwogICAgfQogICAgY291dDw8c29sPDwiXG4iOyAKICAgIHJldHVybiAwOwp9