主要是需要建图,i和j是否可以连边需要比较i的位置是不是每个序列都出现在j的前面,想清楚这点就简单了.
#include<bits/stdc++.h>
using namespace std;#define debug puts("YES");
#define rep(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define ll long long#define lrt int l,int r,int rt
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define root l,r,rt
#define mst(a,b) memset((a),(b),sizeof(a))
#define pii pair<int,int>
#define fi first
#define se second
#define mk(x,y) make_pair(x,y)
const int mod=1e9+7;
const int maxn=1e3+100;
const int ub=1e6;
ll powmod(ll x,ll y){ll t; for(t=1;y;y>>=1,x=x*x%mod) if(y&1) t=t*x%mod; return t;}
ll gcd(ll x,ll y){if(y==0) return x;return gcd(y,x%y);
}
vector<int> g[maxn];
int n,k,a[5][maxn],pos[5][maxn];
int ans=1,dp[maxn];
int judge(int x,int y){rep(i,0,k) if(pos[i][x]>=pos[i][y]) return 0;return 1;
}
int dfs(int x){if(dp[x]) return dp[x];int ret=0;rep(i,0,g[x].size()){ret=max(ret,dfs(g[x][i]));}return dp[x]=1+ret;
}
int main(){cin>>n>>k;rep(i,0,k) rep(j,0,n){cin>>a[i][j];pos[i][a[i][j]]=j+1;}rep(i,1,n+1) rep(j,1,n+1) if(judge(i,j)) g[i].push_back(j);rep(i,1,n+1) ans=max(ans,dfs(i));cout<<ans;return 0;
}