#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void dfs(int u) {

	visited[u] = true;

	for(int v : g[u]) {
		if(!visited[v])
			dfs(v);
		dp[u] = max(dp[u],dp[v]);
	}

	dp[u]++;
}

void solve(int n,int k) {

	g.assign(n,vi());

	loop(i,0,n) {
		loop(j,0,i) {
			bool ins = true;
			loop(x,0,k) {
				if(m[x][i] > m[x][j]) {
					ins = false;
					break;
				}
			}
			if(ins)
				g[i].pb(j);
		}
	}

	int ans = 0;

	loop(u,0,n) {
		if(!visited[u])
			dfs(u);
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	// int u,v;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][inp-1] = j;
		}
	}

	//g.assign(n,vi());

	// loop(i,0,m) {
	// 	cin>>u>>v;
	// 	u-- , v--;
	// 	g[u].pb(v);
	// }

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void dfs(int u) {

	visited[u] = true;

	for(int v : g[u]) {
		if(!visited[v])
			dfs(v);
		dp[u] = max(dp[u],dp[v]);
	}

	dp[u]++;
}

void solve(int n,int k) {

	g.assign(n,vi());

	loop(i,0,n) {
		loop(j,0,n) {
			if(i==j)
				continue;
			bool ins = true;
			loop(x,0,k) {
				if(m[x][i] > m[x][j]) {
					ins = false;
					break;
				}
			}
			if(ins)
				g[i].pb(j);
		}
	}

	int ans = 0;

	loop(u,0,n) {
		if(!visited[u])
			dfs(u);
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	// int u,v;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][inp-1] = j;
		}
	}

	//g.assign(n,vi());

	// loop(i,0,m) {
	// 	cin>>u>>v;
	// 	u-- , v--;
	// 	g[u].pb(v);
	// }

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		loop(v,0,u) {
			bool take = true;
			loop(w,0,k) {
				int a = m[w][u] , b = m[w][v];
				if(pos[w][a] < pos[w][b]) {
					take = false;
					break;
				}
			}
			if(take==false)
				continue;
			dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int dp[1000];

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void dfs(int u) {

	visited[u] = true;

	for(int v : g[u]) {
		if(!visited[v])
			dfs(v);
		dp[u] = max(dp[u],dp[v]);
	}

	dp[u]++;
}

bool isBefore(int *a,int n,int x,int y) {

	int idx;

	loop(i,0,n) {
		if(a[i]==x || a[i]==y) {
			idx = i;
			break;
		}
	}

	return a[idx]==x;
}

void solve(int n,int k) {

	g.assign(n,vi());

	loop(i,1,n+1) {
		loop(j,1,n+1) {
			bool ins = true;
			loop(x,0,k) {
				if(!isBefore(m[x],n,i,j)) {
					ins = false;
					break;
				}
			}
			if(ins)
				g[i-1].pb(j-1);
		}
	}

	// loop(u,0,n) {
	// 	cout<<u+1<<" : ";
	// 	for(int v : g[u])
	// 		cout<<v+1<<" ";
	// 	cout<<"\n";
	// }

	int ans = 0;

	loop(u,0,n) {
		if(!visited[u])
			dfs(u);
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k;
	// int u,v;
	cin>>n>>k;

	loop(i,0,k)
		loop(j,0,n)
			cin>>m[i][j];

	//g.assign(n,vi());

	// loop(i,0,m) {
	// 	cin>>u>>v;
	// 	u-- , v--;
	// 	g[u].pb(v);
	// }

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

// vvi g;
// bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		dp[u] = 1;
		loop(v,0,u) {
			bool take = true;
			loop(x,1,k) {
				int a = m[0][u] , b = m[0][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void dfs(int u) {

	visited[u] = true;

	for(int v : g[u]) {
		if(!visited[v])
			dfs(v);
		dp[u] = max(dp[u],dp[v]);
	}

	dp[u]++;
}

bool isBefore(int *a,int x,int y) {

	return a[x] < a[y];
}

void solve(int n,int k) {

	g.assign(n,vi());

	loop(i,0,n) {
		loop(j,0,n) {
			if(i==j)
				continue;
			bool ins = true;
			loop(x,0,k) {
				if(!isBefore(m[x],i,j)) {
					ins = false;
					break;
				}
			}
			if(ins)
				g[i].pb(j);
		}
	}

	// loop(u,0,n) {
	// 	cout<<u+1<<" : ";
	// 	for(int v : g[u])
	// 		cout<<v+1<<" ";
	// 	cout<<"\n";
	// }

	int ans = 0;

	loop(u,0,n) {
		if(!visited[u])
			dfs(u);
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	// int u,v;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][inp-1] = j;
		}
	}

	//g.assign(n,vi());

	// loop(i,0,m) {
	// 	cin>>u>>v;
	// 	u-- , v--;
	// 	g[u].pb(v);
	// }

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[6][1001];
int pos[6][1001];
int dp[1001];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;
	memset(dp,0);

	loop(u,1,n+1) {
		loop(v,0,u) {
			bool take = true;
			loop(x,1,k+1) {
				int a = m[1][u] , b = m[1][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	// for(int i=1;i<=n;i++){
	// 	for(int j=0;j<i;j++){
	// 		bool g=0;
	// 		for(int l=1;l<=k;l++)if (pos[l][m[1][i]]<pos[l][m[1][j]])g=1;
	// 		if (g)continue;
	// 		dp[i]=max(dp[i],dp[j]+1);
	// 	}
	// 	ans=max(ans,dp[i]);
	// }

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,1,k+1) {
		loop(j,1,n+1) {
			cin>>inp;
			m[i][j] = inp;
			pos[i][inp] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

// vvi g;
// bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		dp[u] = 1;
		loop(v,0,u) {
			bool take = true;
			loop(x,0,k) {
				int a = m[0][u] , b = m[0][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		loop(v,0,u) {
			bool take = false;
			loop(w,0,k) {
				int a = m[w][u] , b = m[w][v];
				if(pos[w][a] < pos[w][b]) {
					take = true;
					continue;
				}
			}
			dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		//dp[u] = 1;
		loop(v,0,u) {
			bool take = true;
			loop(x,0,k) {
				int a = m[0][u] , b = m[0][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void dfs(int u) {

	visited[u] = true;

	for(int v : g[u]) {
		if(!visited[v])
			dfs(v);
		dp[u] = max(dp[u],dp[v]);
	}

	dp[u]++;
}

bool isBefore(int *a,int x,int y) {

	return a[x] < a[y];
}

void solve(int n,int k) {

	g.assign(n,vi());

	loop(i,1,n+1) {
		loop(j,1,n+1) {
			if(i==j)
				continue;
			bool ins = true;
			loop(x,0,k) {
				if(!isBefore(m[x],i,j)) {
					ins = false;
					break;
				}
			}
			if(ins)
				g[i-1].pb(j-1);
		}
	}

	// loop(u,0,n) {
	// 	cout<<u+1<<" : ";
	// 	for(int v : g[u])
	// 		cout<<v+1<<" ";
	// 	cout<<"\n";
	// }

	int ans = 0;

	loop(u,0,n) {
		if(!visited[u])
			dfs(u);
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	// int u,v;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][inp] = j;
		}
	}

	//g.assign(n,vi());

	// loop(i,0,m) {
	// 	cin>>u>>v;
	// 	u-- , v--;
	// 	g[u].pb(v);
	// }

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[5][1000];
int pos[5][1000];
int dp[1000];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;

	loop(u,0,n) {
		dp[u] = 1;
		loop(v,0,u) {
			bool take = true;
			loop(x,0,k) {
				int a = m[0][u] , b = m[0][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,0,k) {
		loop(j,0,n) {
			cin>>inp;
			m[i][j] = inp-1;
			pos[i][inp-1] = j;
		}
	}

	solve(n,k);
}


#include<iostream>
#include<vector>
#include<unordered_map>
#include<queue>
#include<cmath>
#include<bitset>
#include<algorithm>
#include<cstring>
#include<utility>
#include<map>
#include<set>
#include<stack>
#include<unordered_set>
#define loop(i,a,b) for(int i=a;i<b;i++)
#define repeat(n) for(int _=0;_<n;_++)
#define arrInp(a,n) loop(i,0,n) cin>>a[i]
#define all(v) v.begin(),v.end()
#define memset(a,v) memset(a,v,sizeof(a));
#define mod int(1e9+7)
#define MAX int(1e5)
#define pb push_back
#define um unordered_map
#define mp make_pair
#define us unordered_set
#define fastIO ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
using namespace std;

// Codeforces - Substring - #460 div-2 D

typedef long long ll;
typedef vector<int> vi;
typedef vector<vector<int> > vvi;

vvi g;
bitset<MAX> visited;
int m[6][1001];
int pos[6][1001];
int dp[1001];

// we have to use dp array since
// 6 2
// 4 3 1 2 5 6
// 1 4 3 2 5 6

ll min(ll a,ll b) {

	return a < b ? a : b;
}

void solve(int n,int k) {

	int ans = 0;
	memset(dp,0);

	loop(u,1,n+1) {
		loop(v,0,u) {
			bool take = true;
			loop(x,1,k+1) {
				int a = m[1][u] , b = m[1][v];
				if(pos[x][a] < pos[x][b]) {
					take = false;
					break;
				}
			}
			if(take)
				dp[u] = max(dp[u],dp[v]+1);
		}
		ans = max(ans,dp[u]);
	}

	cout<<ans<<"\n";
}

int main() {

	fastIO

	int n,k,inp;
	cin>>n>>k;

	loop(i,1,k+1) {
		loop(j,1,n+1) {
			cin>>inp;
			m[i][j] = inp;
			pos[i][inp] = j;
		}
	}

	solve(n,k);
}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
vector<vector<ll> > adj;
vector<ll> memo;
ll dfs(ll u)
{
	ll ans = 0;
	if(memo[u] != -1) return memo[u];
	for(ll x : adj[u])
		ans = max( ans , dfs(x));
	return memo[u] = ans + 1;
}
int main()
{
    //fast();
    ll n , k;
    cin>>n>>k;
    ll arr[k][n] , pos[k][n];
    adj.resize(n + 1);
    memo.resize(n + 1 , -1);
    for(ll i = 0 ; i < k ; i++)
    	for(ll j = 0 ; j < n ; j++ )
		{
			cin>>arr[i][j];
			pos[i][arr[i][j] - 1] = j;
		}
    for(ll i = 0 ; i < n ; i++ )
    {
    	for(ll j = 0 ; j < n ; j++ )
    	{
    		ll flag = 1;
    		for(ll l = 0 ; l < k ; l++ )
    			if(pos[l][i] >= pos[l][j])
    			{
    				flag = 0;
    				break;
    			}
    		if(flag)
    			adj[i].pb(j);
    	}
    }
 //    for(ll i = 0 ; i < n ; i++)
	// {
	// 	cout<<adj[i].size()<<" : ";
	// 	for(ll x : adj[i])
 //    		cout<<x<<" ";
	// 	cout<<endl;
	// }
    ll ans = 0;
    for(ll i = 0 ; i < n ; i++)
    	ans = max(ans , dfs(i));
    cout<<ans<<endl;

}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll int
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
ll n, k;
vector<ll> solve(vector<ll> a , vector<ll> b)
{
	ll dp[a.size() + 1][b.size() + 1];
	memset(dp , 0 , sizeof dp);
	for(ll i = 1 ; i <= a.size() ; i++ )
	{
		for(ll j = 1 ; j <= b.size() ; j++ )
		{
			if(a[i - 1] == b[j - 1])
				dp[i][j] = 1 + dp[i - 1][j - 1];
			dp[i][j] =max( dp[i][j] ,max(dp[i - 1][j] , dp[i][j - 1]));
			// cout<<dp[i][j]<<" ";
		}
		// cout<<endl;
	}
	vector<ll> res;
	ll x = a.size() , y = b.size() ;
	while(x != 0 )
	{
				// cerr<<x<<" "<<y<<endl;

		if(dp[x - 1][y] == dp[x][y])
		{
			x--;
		}
		else if(dp[x][y - 1] == dp[x][y])
		{
			y--;
		}
		else
		{
			res.pb(a[x - 1]);
			x--; y--;
		}
	}
	reverse(res.begin() , res.end());
	return res;
}
int main()
{
    fast();
    cin>>n>>k;
    vector<ll> arr[k];
    for(ll j = 0 ; j < k ; j++)
    	for(ll i = 0 ; i < n ; i++ )
    	{
    		ll x ;
    		cin>>x;
    		arr[j].pb(x);
    	}
    ll ans = 0;
    for(ll i = 0 ; i < k ; i++)
    {
    	for(ll j = i + 1 ; j < k ; j++ )
    	{
    		vector<ll> res = solve(arr[i] , arr[j]);
    		for(ll m = 0 ; m < k ; m++)
    		{
    			if( m != j && m != i)
    				res = solve(res , arr[m]);
    		}
    		ans = max(ans ,(ll)res.size());
    	}
    }
    cout<<ans<<endl;
}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
int main()
{
    //fast();
    ll n , k;
    cin>>n>>k;
    ll arr[k][n + 5] , pos[k][n + 5], dp[n + 5] = {0};
    for(ll i = 0 ; i < k ; i++ )
    {
    	for(ll j = 1 ; j <= n ; j++ )
    	{
    		cin>>arr[i][j];
    		pos[i][arr[i][j]] = j;
    	}
    }
    ll ans = 0;
    for(ll i = 1 ; i <= n ; i++ )
    {
    	ll num = 0;
    	for(ll j = 1 ; j < i ; j++ )
    	{
    		ll flag = 1;
    		for(ll l = 0 ; l < k ; l++ )
    		{
    			if(pos[ l ][ arr[0][i] ] < pos[ l ][ arr[0][j] ])
    			{
    				flag = 0;
    				break;
    			}
    		}
    		if(flag) num = max(num , dp[j]);
    	}
    	dp[i] = num + 1;
    	ans = max(ans , dp[i]);
    }
    cout<<ans<<endl;
}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll int
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
ll n, k;
vector<ll> solve(vector<ll> a , vector<ll> b)
{
	ll dp[a.size() + 1][b.size() + 1];
	memset(dp , 0 , sizeof dp);
	for(ll i = 1 ; i <= a.size() ; i++ )
	{
		for(ll j = 1 ; j <= b.size() ; j++ )
		{
			if(a[i - 1] == b[j - 1])
				dp[i][j] = 1 + dp[i - 1][j - 1];
			dp[i][j] =max( dp[i][j] ,max(dp[i - 1][j] , dp[i][j - 1]));
			// cout<<dp[i][j]<<" ";
		}
		// cout<<endl;
	}
	vector<ll> res;
	ll x = a.size() , y = b.size() ;
	while(x != 0 )
	{
				// cerr<<x<<" "<<y<<endl;

		if(dp[x - 1][y] == dp[x][y])
		{
			x--;
		}
		else if(dp[x][y - 1] == dp[x][y])
		{
			y--;
		}
		else
		{
			res.pb(a[x - 1]);
			x--; y--;
		}
	}
	reverse(res.begin() , res.end());
	return res;
}
int main()
{
    fast();
    cin>>n>>k;
    vector<ll> a(n) , b(n);
    for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    for(ll i = 0 ; i < n ; i++ )
    	cin>>b[i];
    vector<ll> ans = solve(a ,b);
    // for(ll i = 0 ; i < ans.size() ; i++ )
    // 	cout<<ans[i]<<" ";
    for(ll i = 2 ; i < k ; i++)
    {
    	for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    	ans = solve(ans , a);
    }
    cout<<ans.size()<<endl;
}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll int
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
ll n, k;
vector<ll> solve(vector<ll> a , vector<ll> b)
{
	ll dp[a.size() + 1][b.size() + 1];
	memset(dp , 0 , sizeof dp);
	for(ll i = 1 ; i <= a.size() ; i++ )
	{
		for(ll j = 1 ; j <= b.size() ; j++ )
		{
			if(a[i - 1] == b[j - 1])
				dp[i][j] = 1 + dp[i - 1][j - 1];
			else
				dp[i][j] = max(dp[i - 1][j] , dp[i][j - 1]);
			// cout<<dp[i][j]<<" ";
		}
		// cout<<endl;
	}
	vector<ll> res;
	ll x = a.size() , y = b.size() ;
	while(x != 0 )
	{
				// cerr<<x<<" "<<y<<endl;

		if(dp[x - 1][y] == dp[x][y])
		{
			x--;
		}
		else if(dp[x][y - 1] == dp[x][y])
		{
			y--;
		}
		else
		{
			res.pb(a[x - 1]);
			x--; y--;
		}
	}
	reverse(res.begin() , res.end());
	return res;
}
int main()
{
    fast();
    cin>>n>>k;
    vector<ll> a(n) , b(n);
    for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    for(ll i = 0 ; i < n ; i++ )
    	cin>>b[i];
    vector<ll> ans = solve(a ,b);
    // for(ll i = 0 ; i < ans.size() ; i++ )
    // 	cout<<ans[i]<<" ";
    for(ll i = 2 ; i < k ; i++)
    {
    	for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    	ans = solve(ans , a);
    }
    cout<<ans.size()<<endl;
}


/*-----rk_98-----*/
#include<bits/stdc++.h>
#define fast() ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll int
#define mod 1000000007
#define inf (1ll<<60)
#define eps 1e-9
#define ff first
#define ss second
#define pb push_back
#define endl "\n"
using namespace std;
ll n, k;
vector<ll> solve(vector<ll> a , vector<ll> b)
{
	ll dp[a.size() + 1][b.size() + 1];
	memset(dp , 0 , sizeof dp);
	for(ll i = 1 ; i <= a.size() ; i++ )
	{
		for(ll j = 1 ; j <= b.size() ; j++ )
		{
			if(a[i - 1] == b[j - 1])
				dp[i][j] = 1 + dp[i - 1][j - 1];
			dp[i][j] =max( dp[i][j] ,max(dp[i - 1][j] , dp[i][j - 1]));
			// cout<<dp[i][j]<<" ";
		}
		// cout<<endl;
	}
	vector<ll> res;
	ll x = a.size() , y = b.size() ;
	while(x != 0 )
	{
				// cerr<<x<<" "<<y<<endl;

		if(dp[x - 1][y] == dp[x][y])
		{
			x--;
		}
		else if(dp[x][y - 1] == dp[x][y])
		{
			y--;
		}
		else
		{
			res.pb(a[x - 1]);
			x--; y--;
		}
	}
	reverse(res.begin() , res.end());
	return res;
}
int main()
{
    fast();
    cin>>n>>k;
    vector<ll> a(n) , b(n);
    for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    for(ll i = 0 ; i < n ; i++ )
    	cin>>b[i];
    vector<ll> ans = solve(a ,b);
    // for(ll i = 0 ; i < ans.size() ; i++ )
    // 	cout<<ans[i]<<" ";
    for(ll j = 2 ; j < k ; j++)
    {
    	for(ll i = 0 ; i < n ; i++ )
    	cin>>a[i];
    	ans = solve(ans , a);
    }
    cout<<ans.size()<<endl;
}


#include<bits/stdc++.h>
using namespace std;

vector<int> m_v(int n){
	vector<int> arr(n,-1);
	return arr;
}

int dfs(int u,vector<vector<int> > &graph,vector<int> &depth,vector<int> &visited){
	if(visited[u]==1){
		return depth[u];
	}
	else{
		int maxi=0;
		for(int i=0;i<graph[u].size();i++){
			maxi=max(dfs(graph[u][i],graph,depth,visited),maxi);
		}
		depth[u]=maxi+1;
		return (maxi+1);

	}
}
int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int> > positions(n,m_v(n));
	vector<vector<int> > graph(n);
	vector<int> depth(n,-1);
	int t;
	for(int i=0;i<k;i++){
		for(int j=0;j<n;j++){
			cin>>t;
			positions[i][t-1]=j;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j){
				int flag=1;
				for(int r=0;r<k;r++){
					if(positions[r][j]<positions[r][i]){
						flag=0;
						break;
					}
				}
				if(flag){
					graph[i].push_back(j);
				}
			}
		}
	}

	vector<int> visited(n,-1);
	int ans=1;
	for(int i=0;i<n;i++){
		ans=max(ans,dfs(i,graph,depth,visited));
	}
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> m_v(int n){
	vector<int> arr(n,-1);
	return arr;
}

int dfs(int u,vector<vector<int> > &graph,vector<int> &depth,vector<int> &visited){
	if(visited[u]==1){
		return depth[u];
	}
	else{
		int maxi=0;
		for(int i=0;i<graph[u].size();i++){
			maxi=max(dfs(graph[u][i],graph,depth,visited),maxi);
		}
		depth[u]=maxi+1;
		return (maxi+1);

	}
}
int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int> > positions(k,m_v(n));
	vector<vector<int> > graph(n);
	vector<int> depth(n,-1);
	int t;
	for(int i=0;i<k;i++){
		for(int j=0;j<n;j++){
			cin>>t;
			positions[i][t-1]=j;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j){
				int flag=1;
				for(int r=0;r<k;r++){
					if(positions[r][j]<positions[r][i]){
						flag=0;
						break;
					}
				}
				if(flag){
					graph[i].push_back(j);
				}
			}
		}
	}

	vector<int> visited(n,-1);
	int ans=1;
	for(int i=0;i<n;i++){
		ans=max(ans,dfs(i,graph,depth,visited));
	}
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> m_v(int n){
	vector<int> arr(n,-1);
	return arr;
}

/*
int dfs(int u,vector<vector<int> > &graph,vector<int> &depth,vector<int> &visited){
	if(visited[u]==1){
		return depth[u];
	}
	else{
		int maxi=0;
		for(int i=0;i<graph[u].size();i++){
			maxi=max(dfs(graph[u][i],graph,depth,visited),maxi);
		}
		depth[u]=maxi+1;
		return (maxi+1);

	}
}
*/
int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int> > positions(k,m_v(n));
	/*vector<vector<int> > graph(n);*/
	//vector<int> incident(n,-1);
	vector<int> depth(n,1);
	int t;
	for(int i=0;i<k;i++){
		for(int j=0;j<n;j++){
			cin>>t;
			positions[i][t-1]=j;
		}
	}
	int ans=1;
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j){
				int flag=1;
				for(int r=0;r<k;r++){
					if(positions[r][j]<positions[r][i]){
						flag=0;
						break;
					}
				}
				if(flag){
					//graph[i].push_back(j);
					depth[j]=max(depth[i]+1,depth[j]);
					ans=max(ans,depth[j]);
					//incident[j]=1;
				}
			}
		}
	}
/*
	vector<int> visited(n,-1);
	int ans=1;
	for(int i=0;i<n;i++){
		if(incident[i]!=1)
			ans=max(ans,dfs(i,graph,depth,visited));
	}

	*/
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> m_v(int n){
	vector<int> arr(n,-1);
	return arr;
}

int dfs(int u,vector<vector<int> > &graph,vector<int> &depth,vector<int> &visited){
	if(visited[u]==1){
		return depth[u];
	}
	else{
		visited[u]=1;
		int maxi=0;
		for(int i=0;i<graph[u].size();i++){
			maxi=max(dfs(graph[u][i],graph,depth,visited),maxi);
		}
		depth[u]=maxi+1;
		return (maxi+1);

	}
}
int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int> > positions(k,m_v(n));
	vector<vector<int> > graph(n);
	vector<int> incident(n,-1);
	vector<int> depth(n,-1);
	int t;
	for(int i=0;i<k;i++){
		for(int j=0;j<n;j++){
			cin>>t;
			positions[i][t-1]=j;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j){
				int flag=1;
				for(int r=0;r<k;r++){
					if(positions[r][j]<positions[r][i]){
						flag=0;
						break;
					}
				}
				if(flag){
					graph[i].push_back(j);
					incident[j]=1;
				}
			}
		}
	}

	vector<int> visited(n,-1);
	int ans=1;
	for(int i=0;i<n;i++){
		if(incident[i]!=1)
		ans=max(ans,dfs(i,graph,depth,visited));
	}
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;

vector<int> m_v(int n){
	vector<int> arr(n,-1);
	return arr;
}

int dfs(int u,vector<vector<int> > &graph,vector<int> &depth,vector<int> &visited){
	if(visited[u]==1){
		return depth[u];
	}
	else{
		int maxi=0;
		for(int i=0;i<graph[u].size();i++){
			maxi=max(dfs(graph[u][i],graph,depth,visited),maxi);
		}
		depth[u]=maxi+1;
		return (maxi+1);

	}
}
int main(){
	int n,k;
	cin>>n>>k;
	vector<vector<int> > positions(k,m_v(n));
	vector<vector<int> > graph(n);
	vector<int> incident(n,-1);
	vector<int> depth(n,-1);
	int t;
	for(int i=0;i<k;i++){
		for(int j=0;j<n;j++){
			cin>>t;
			positions[i][t-1]=j;
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			if(i!=j){
				int flag=1;
				for(int r=0;r<k;r++){
					if(positions[r][j]<positions[r][i]){
						flag=0;
						break;
					}
				}
				if(flag){
					graph[i].push_back(j);
					incident[j]=1;
				}
			}
		}
	}

	vector<int> visited(n,-1);
	int ans=1;
	for(int i=0;i<n;i++){
		if(incident[i]!=1)
			ans=max(ans,dfs(i,graph,depth,visited));
	}
	cout<<ans<<endl;

}


#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long double ld;
typedef string str;
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pl> vpl;

#define rep(i,a,b) for (ll i = (a); i < (b); i++)
#define repd(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define trav(a,x) for (auto& a : x)

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define ins insert
#define pcount(x)  __builtin_popcount(x);

const int mod = 1e9+7;
const ll inf = 1e12;
const ll N= 200005;
unordered_map<ll,list<ll>>mp;
ll ans[1005];
ll ans1=1;
ll dfs(ll a,bool visited[]){
    visited[a]=true;
    for(auto j:mp[a]){
        if(!visited[j]){
            dfs(j,visited);
            ans[a]=max(ans[a],ans[j]+1);
        }
    }
    ans1=max(ans1,ans[a]);
}
int solve(){
    ll n,k,a;
    cin>>n>>k;
    ll pos[k][n+1];
    for(ll i=0;i<k;i++){
        for(ll j=0;j<n;j++){
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(ll i=1;i<=n;i++){
        ans[i]=1;
        for(ll j=1;j<=n;j++){
            if(i==j)continue;
            bool flag=true;
            for(ll p=0;p<k;p++){
                if(pos[p][i]>pos[p][j]){
                    flag=false;
                }
            }
            if(flag){
                mp[i].pb(j);
            }
        }
    }
    bool visited[n+1];
    memset(visited,false,sizeof visited);
    for(ll i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i,visited);
        }
    }
    cout<<ans1;
    return 0;
}
int main(){
    int t;
    t=1;
    while(t){
        solve();
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long double ld;
typedef string str;
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pl> vpl;

#define rep(i,a,b) for (ll i = (a); i < (b); i++)
#define repd(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define trav(a,x) for (auto& a : x)

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define ins insert
#define pcount(x)  __builtin_popcount(x);

const int mod = 1e9+7;
const ll inf = 1e12;
const ll N= 200005;
unordered_map<ll,list<ll>>mp;
ll ans=1;
ll dfs(ll a,bool visited[],ll cnt){
    visited[a]=true;
    ans=max(ans,cnt);
    for(auto j:mp[a]){
        if(!visited[j]){
            dfs(j,visited,cnt+1);
        }
    }
}
int solve(){
    ll n,k,a;
    cin>>n>>k;
    ll pos[k][n+1];
    for(ll i=0;i<k;i++){
        for(ll j=0;j<n;j++){
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(i==j)continue;
            bool flag=true;
            for(ll p=0;p<k;p++){
                if(pos[p][i]>pos[p][j]){
                    flag=false;
                }
            }
            if(flag){
                mp[i].pb(j);
                mp[j].pb(i);
            }
        }
    }
    bool visited[n+1];
    memset(visited,false,sizeof visited);
    for(ll i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i,visited,0);
        }
    }
    cout<<ans;
    return 0;
}
int main(){
    int t;
    t=1;
    while(t){
        solve();
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long double ld;
typedef string str;
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pl> vpl;

#define rep(i,a,b) for (ll i = (a); i < (b); i++)
#define repd(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define trav(a,x) for (auto& a : x)

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define ins insert
#define pcount(x)  __builtin_popcount(x);

const int mod = 1e9+7;
const ll inf = 1e12;
const ll N= 200005;
unordered_map<ll,list<ll>>mp;
ll ans=1;
ll dfs(ll a,bool visited[],ll cnt){
    visited[a]=true;
    ans=max(ans,cnt);
    for(auto j:mp[a]){
        if(!visited[j]){
            dfs(j,visited,cnt+1);
        }
    }
}
int solve(){
    ll n,k,a;
    cin>>n>>k;
    ll pos[k][n+1];
    for(ll i=0;i<k;i++){
        for(ll j=0;j<n;j++){
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(i==j)continue;
            bool flag=true;
            for(ll p=0;p<k;p++){
                if(pos[p][i]>pos[k][j]){
                    flag=false;
                }
            }
            if(flag){
                mp[i].pb(j);
                mp[j].pb(i);
            }
        }
    }
    bool visited[n+1];
    memset(visited,false,sizeof visited);
    for(ll i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i,visited,0);
        }
    }
    cout<<ans;
    return 0;
}
int main(){
    int t;
    t=1;
    while(t){
        solve();
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long double ld;
typedef string str;
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pl> vpl;

#define rep(i,a,b) for (ll i = (a); i < (b); i++)
#define repd(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define trav(a,x) for (auto& a : x)

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define ins insert
#define pcount(x)  __builtin_popcount(x);

const int mod = 1e9+7;
const ll inf = 1e12;
const ll N= 200005;
unordered_map<ll,list<ll>>mp;
ll ans[1005];
ll ans1=1;
ll dfs(ll a,bool visited[]){
    visited[a]=true;
    for(auto j:mp[a]){
        if(!visited[j]){
            dfs(j,visited);

        }
        ans[a]=max(ans[a],ans[j]+1);
    }
    ans1=max(ans1,ans[a]);
}
int solve(){
    ll n,k,a;
    cin>>n>>k;
    ll pos[k][n+1];
    for(ll i=0;i<k;i++){
        for(ll j=0;j<n;j++){
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(ll i=1;i<=n;i++){
        ans[i]=1;
        for(ll j=1;j<=n;j++){
            if(i==j)continue;
            bool flag=true;
            for(ll p=0;p<k;p++){
                if(pos[p][i]>=pos[p][j]){
                    flag=false;
                }
            }
            if(flag){
                mp[i].pb(j);
            }
        }
    }
    bool visited[n+1];
    memset(visited,false,sizeof visited);
    for(ll i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i,visited);
        }
    }
    cout<<ans1;
    return 0;
}
int main(){
    int t;
    t=1;
    while(t){
        solve();
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;
typedef double db;
typedef long double ld;
typedef string str;
#define ll long long int
typedef pair<int,int> pii;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;

typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<ld> vd;
typedef vector<str> vs;
typedef vector<pii> vpi;
typedef vector<pl> vpl;

#define rep(i,a,b) for (ll i = (a); i < (b); i++)
#define repd(i,a,b) for (ll i = (b)-1; i >= (a); i--)
#define trav(a,x) for (auto& a : x)

#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound

#define sz(x) (int)x.size()
#define all(x) begin(x), end(x)
#define ins insert
#define pcount(x)  __builtin_popcount(x);

const int mod = 1e9+7;
const ll inf = 1e12;
const ll N= 200005;
unordered_map<ll,list<ll>>mp;
ll ans=1;
ll dfs(ll a,bool visited[],ll cnt){
    visited[a]=true;
    ans=max(ans,cnt);
    for(auto j:mp[a]){
        if(!visited[j]){
            dfs(j,visited,cnt+1);
        }
    }
}
int solve(){
    ll n,k,a;
    cin>>n>>k;
    ll pos[k][n+1];
    for(ll i=0;i<k;i++){
        for(ll j=0;j<n;j++){
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(ll i=1;i<=n;i++){
        for(ll j=1;j<=n;j++){
            if(i==j)continue;
            bool flag=true;
            for(ll p=0;p<k;p++){
                if(pos[p][i]>pos[p][j]){
                    flag=false;
                }
            }
            if(flag){
                mp[i].pb(j);
            }
        }
    }
    bool visited[n+1];
    memset(visited,false,sizeof visited);
    for(ll i=1;i<=n;i++){
        if(!visited[i]){
            dfs(i,visited,1);
        }
    }
    cout<<ans;
    return 0;
}
int main(){
    int t;
    t=1;
    while(t){
        solve();
        t--;
    }
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
#define int long long

//memset(dp,0x3f,sizeof(dp));

int n,k;
vector<vector<int>> ind(6,vector<int> (1005));
vector<vector<int>> a(6);
vector<vector<int>> adj(1005);
int m,ans;

int dfs(int a){
    int ma=0;
    for(auto ac:adj[a]){
        ma=max(ma,(dfs(ac)+1));
    }
    return ma;
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        cin>>n>>k;
        int tmp;
        ind.clear();
        a.clear();
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                cin>>tmp;
                a[i].pb(tmp);
                ind[i][tmp]=j;

            }
        }

        adj.clear();
        bool flag;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        // int visited[n+1];
        // memset(visited,false,sizeof(visited));

        int ans=1;
        for(int i=0;i<n;i++){
            // if(!visited[i]){
            //     visited[i]=true;
                ans=max(dfs(i)+1,ans);

            // }
        }
        cout<<ans<<"\n";

    }

    end_routine();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
#define int long long

//memset(dp,0x3f,sizeof(dp));

int n,k;
vector<vector<int>> ind(5,vector<int> (1001));
vector<vector<int>> a(5);
vector<vector<int>> adj(1001);
int m,ans;

int dfs(int a){
    int ma=0;
    for(auto ac:adj[a]){
        ma=max(ma,(dfs(ac)+1));
    }
    return ma;
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        cin>>n>>k;
        int tmp;
        ind.clear();
        a.clear();
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                cin>>tmp;
                a[i].pb(tmp);
                ind[i][tmp]=j;

            }
        }

        adj.clear();
        bool flag;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        // int visited[n+1];
        // memset(visited,false,sizeof(visited));

        int ans=1;
        for(int i=0;i<n;i++){
            // if(!visited[i]){
            //     visited[i]=true;
                ans=max(dfs(i)+1,ans);

            // }
        }
        cout<<ans<<"\n";

    }

    end_routine();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
// #define int long long

//memset(dp,0x3f,sizeof(dp));

int n,k;
// vector<vector<int>> a(6);
vector<vector<int>> adj(1002);
int m,ans;

int dfs(int a){
    int ma=0;
    for(auto ac:adj[a]){
        ma=max(ma,(dfs(ac)+1));
    }
    return ma;
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        // cin>>n>>k;
        sd(n);
        sd(k);
        int tmp;
        // ind.clear();
        // vector<int> ind[n+5];
        vector<vector<int>> ind(k+1,vector<int>(n+5));
        // vector<int> temp;
        // for(int i=0;i<=n;i++){
        //     adj.pb(temp);
        // }
        // for(int i=0;i<=n;i++){
        //     temp.pb(0);
        // }
        // for(int i=0;i<k;i++){
        //     ind.pb(temp);
        // }
        // as.clear();
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                // cin>>tmp;
                sd(tmp);
                // a[i].pb(tmp);
                ind[i][tmp]=j;

            }
        }

        // for(int i=0;i<k;i++)
        //     for(int j=0;j<n;j++)
        //         ind[i][a[i][j]]=j;

        adj.clear();
        bool flag;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        // int visited[n+1];
        // memset(visited,false,sizeof(visited));

        int sta=0;
        int l=0;
        for(int i=0;i<=n;i++){
            if(l<adj[i].size()){
                l=adj[i].size();
                sta=i;
            }
        }

        int ans=1;
        ans=max(dfs(sta)+1,ans);
        cout<<ans<<"\n";

    }

    end_routine();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
#define int long long

//memset(dp,0x3f,sizeof(dp));

int n,k;
vector<vector<int>> ind(6,vector<int> (1001));
vector<vector<int>> a(6);
vector<vector<int>> adj(1001);
int m,ans;

int dfs(int a){
    int ma=0;
    for(auto ac:adj[a]){
        ma=max(ma,(dfs(ac)+1));
    }
    return ma;
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        cin>>n>>k;
        int tmp;
        ind.clear();
        a.clear();
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                cin>>tmp;
                a[i].pb(tmp);
                // ind[i][tmp]=j;

            }
        }

        for(int i=0;i<k;i++)
            for(int j=0;j<n;j++)
                ind[i][a[i][j]]=j;

        adj.clear();
        bool flag;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        // int visited[n+1];
        // memset(visited,false,sizeof(visited));

        int ans=1;
        for(int i=0;i<n;i++){
            // if(!visited[i]){
            //     visited[i]=true;
                ans=max(dfs(i)+1,ans);

            // }
        }
        cout<<ans<<"\n";

    }

    end_routine();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
// #define int long long

//memset(dp,0x3f,sizeof(dp));
int dp[1005];
int n,k;
vector<vector<int>> adj(1002);
int ans;

int dfs(int a){
    int ma=0,cmp;
    for(auto ac:adj[a]){
        if(dp[ac]==-1){
            cmp=dfs(ac)+1;
        }
        else{
            cmp=dp[ac]+1;
        }
        ma=max(ma,cmp);
    }
    return (dp[a]=ma);
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        // cin>>n>>k;
        sd(n);
        sd(k);
        int tmp;
        vector<vector<int>> ind(k+1,vector<int>(n+5));
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                sd(tmp);
                ind[i][tmp]=j;

            }
        }

        adj.clear();
        bool flag;
        memset(dp,-1,sizeof(dp));
        bool visited[n+1];
        memset(visited,true,sizeof(visited));

        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                        break;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                    visited[j]=false;
                    // trace(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        int sta=0;
        int l=0;
        for(int i=0;i<=n;i++){
            if(l<adj[i].size()){
                l=adj[i].size();
                sta=i;
            }
        }

        int ans=1;

        for(int i=0;i<n;i++){
            if(visited[i]){
                trace(i);
                ans=max(ans,(dfs(i)+1));
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<dp[i]<<" ";
        // }
        cout<<(ans)<<"\n";

    }

    end_routine();
    return 0;
}


#include<bits/stdc++.h>
using namespace std;

#define sd(mark) scanf("%d",&mark)
#define ss(mark) scanf("%s",mark)
#define clr(mark) memset(mark,0,sizeof(mark))

#ifndef strange
#define trace(...)
// #define endl '\n'
#define start_routine()
#define end_routine()
#endif
typedef long long ll;
//__gcd

#define mod 1000000007
#define F first
#define S second
#define mp make_pair
#define all(x) x.begin(), x.end()
#define trav(a,x) for(auto &a:x)
#define pii pair<int,int>
#define sz(x)  (long long (x.size()))

#define pb push_back
#define int long long

//memset(dp,0x3f,sizeof(dp));

int n,k;
// vector<vector<int>> a(6);
vector<vector<int>> adj;
int m,ans;

int dfs(int a){
    int ma=0;
    for(auto ac:adj[a]){
        ma=max(ma,(dfs(ac)+1));
    }
    return ma;
}

signed main(){
    int t=1;
    start_routine();
    cin.tie(0); cout.tie(0);
    ios_base::sync_with_stdio(NULL);
    cout<<fixed<<setprecision(10);

    // string a,b;
    // cin>>t;
    while(t-->0){
        cin>>n>>k;
        int tmp;
        // ind.clear();
        // vector<int> ind[n+5];
        vector<vector<int>> ind;
        vector<int> temp;
        for(int i=0;i<=n;i++){
            adj.pb(temp);
        }
        for(int i=0;i<=n;i++){
            temp.pb(0);
        }
        for(int i=0;i<k;i++){
            ind.pb(temp);
        }
        // as.clear();
        for(int i=0;i<k;i++){
            for(int j=0;j<n;j++){

                cin>>tmp;
                // a[i].pb(tmp);
                ind[i][tmp]=j;

            }
        }

        // for(int i=0;i<k;i++)
        //     for(int j=0;j<n;j++)
        //         ind[i][a[i][j]]=j;

        adj.clear();
        bool flag;
        for(int i=0;i<n;i++){
            for(int j=0;j<n;j++){
                flag=true;
                for(int h=0;h<k;h++){
                    if(ind[h][i+1]<ind[h][j+1]){
                        continue;
                    }
                    else{
                        flag=false;
                    }

                }

                if(flag){
                    adj[i].pb(j);
                }
            }
        }

        // for(int i=0;i<n;i++){
        //     cout<<i+1<<" -> ";

        //     for(auto f:adj[i]){
        //         cout<<f+1<<" ";
        //     }
        //     cout<<"\n";
        // }

        // int visited[n+1];
        // memset(visited,false,sizeof(visited));

        int ans=1;
        for(int i=0;i<n;i++){
            // if(!visited[i]){
            //     visited[i]=true;
                ans=max(dfs(i)+1,ans);

            // }
        }
        cout<<ans<<"\n";

    }

    end_routine();
    return 0;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int res,ind[maxn][10],dp[maxn];
vector<int>g[maxn],pos;
bool mrk[maxn];

void dfs(int v){
	mrk[v]=true;
	for(auto u:g[v])
		if(mrk[u]==false)dfs(u);
	pos.push_back(v);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)if(mrk[i]==false)dfs(i);
	for(auto i:pos)
		for(auto u:g[i])
			dp[i]=max(dp[u]+1,dp[i]),res=max(res,dp[i]);
	cout<<res+1;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int res,ind[maxn][6];
vector<int>g[maxn];

void dfs(int v,int h){
	res=max(res,h);
	for(auto u:g[v])dfs(u,h+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,1);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int res,ind[maxn][6];
vector<int>g[maxn];
bool mrk[maxn][maxn];

void dfs(int v,int h,int i){
	mrk[v][i]=true;
	res=max(res,h);
	for(auto u:g[v])if(mrk[u][i]==false)dfs(u,h+1,i);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,1,i);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define ld long double
#define int long long
#define F first
#define S second
using namespace std;
void null(){return ;}
const int INF=3e18+9237;
const int maxn=1e3+7;
const int mod=1e9+7;
const int sqN=320;
const int lgN=20;
int res,ind[maxn][10];
vector<int>g[maxn];

void dfs(int v,int h){
	res=max(res,h);
	for(auto u:g[v])dfs(u,h+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,0);
	cout<<res+1;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
#define ld long double
// #define int long long
#define F first
#define S second
using namespace std;
void null(){return ;}
const int INF=3e18+9237;
const int maxn=1e3+7;
const int mod=1e9+7;
const int sqN=320;
const int lgN=20;
int res,ind[maxn][10];
vector<int>g[maxn];

void dfs(int v,int h){
	res=max(res,h);
	for(auto u:g[v])dfs(u,h+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,0);
	cout<<res+1;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int res,ind[maxn][6];
vector<int>g[maxn];
bool mrk[maxn];

void dfs(int v,int h){
	mrk[v]=true;
	res=max(res,h);
	for(auto u:g[v])if(mrk[u]==false)dfs(u,h+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,1),fill(mrk,mrk+n+1,false);
	cout<<res;
}


///age yekam bekeshi beham is no problem ._.
#include<bits/stdc++.h>
using namespace std;
const int maxn=1e3+7;
int res,ind[maxn][6];
vector<int>g[maxn];
bool mrk[maxn];

void dfs(int v,int h){
	mrk[v]=true;
	res=max(res,h);
	for(auto u:g[v])if(mrk[u]==false)dfs(u,h+1);
}

int32_t main(){
    ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
	int n,k;cin>>n>>k;
	for(int i=0;i<k;i++)
		for(int j=0,a;j<n;j++)
			cin>>a,ind[a][i]=j;
	for(int i=1;i<=n;i++){
		for(int j=1;j<=n;j++){
			if(i==j)continue;
			bool c=true;
			for(int o=0;o<k;o++)
				if(ind[i][o]<ind[j][o])c=false;
			if(c)g[j].push_back(i);
		}
	}
	for(int i=1;i<=n;i++)dfs(i,1);
	cout<<res;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x,int l)
{
    vis[x]=true;

    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u,l+1);
    }
    mx=max(mx,l);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
            else gr[j].push_back(i);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i,1);
        memset(vis,0,sizeof vis);
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x,int l)
{
    vis[x]=true;
    mx=max(mx,l);
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u,l+1);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i,1);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            v.push_back(x);
        }
        for(int i=0; i<n-1; i++)
        {
            int x=v[i];
            for(int j=i+1; j<n; j++)
            {
                int xx=v[j];
                edg[x][xx]++;
            }
        }
        v.clear();
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(edg[i][j]==kk)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        if(vis[i]==false)dfs(i);
        mx=max(ans,mx);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010],dis[1001];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x,int l)
{
    vis[x]=true;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)
        {
            dfs(u,l+1);
            vis[u]=false;
        }
    }
    mx=max(mx,l);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=1; i<=n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i,1);
        memset(vis,0,sizeof vis);
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans;
vector<int>v,gr[100000];
bool edg[1001][1001],vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0;i<gr[x].size();i++)
    {
        int u=gr[x][i];
        if(vis[u]==false&&edg[u][x]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    while(k--)
    {
        v.clear();
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            v.push_back(x);
        }
        for(int i=0;i<n-1;i++)
        {
            int x=v[i];
            for(int j=i+1;j<n;j++)
            {
                int xx=v[j];
                gr[x].push_back(xx);
                edg[x][xx]=true;
            }
        }
    }
    dfs(v[0]);
    /*for(int i=1;i<=n;i++)
    {
        cout<<' '<<i<<endl;
        for(int j=0;j<gr[i].size();j++)cout<<gr[i][j]<<' ';
        cout<<endl;
    }*/
    cout<<ans;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
        v.clear();
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            bool t=false;
            for(int z=1;z<=kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx;
vector<int>v,gr[100000];
bool edg[1001][1001],vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0;i<gr[x].size();i++)
    {
        int u=gr[x][i];
        if(vis[u]==false&&edg[u][x]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    while(k--)
    {
        v.clear();
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            v.push_back(x);
        }
        for(int i=0;i<n-1;i++)
        {
            int x=v[i];
            for(int j=i+1;j<n;j++)
            {
                int xx=v[j];
                gr[x].push_back(xx);
                edg[x][xx]=true;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        dfs(v[i]);
        mx=max(ans,mx);
        ans=0;
    }
    /*for(int i=1;i<=n;i++)
    {
        cout<<' '<<i<<endl;
        for(int j=0;j<gr[i].size();j++)cout<<gr[i][j]<<' ';
        cout<<endl;
    }*/
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x,int l)
{
    vis[x]=true;

    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u,l+1);
    }
    mx=max(mx,l);
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
            t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[j][z]>edg[i][z])t=true;
            }
            if(t==false)gr[j].push_back(i);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i,1);
        memset(vis,0,sizeof vis);
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        v.clear();
        for(int i=0; i<n; i++)
        {
            int x;
            cin>>x;
            v.push_back(x);
        }
        for(int i=0; i<n-1; i++)
        {
            int x=v[i];
            for(int j=i+1; j<n; j++)
            {
                int xx=v[j];
                edg[x][xx]++;
            }
        }
    }

    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(edg[i][j]==kk)gr[i].push_back(j);
        }
    }
    for(int i=1;i<=n;i++)
    {
        dfs(i);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx;
vector<int>v,gr[100000];
bool edg[1001][1001],vis[1001];
void dfs(int x)
{
    vis[x]=true;
    ans++;
    for(int i=0;i<gr[x].size();i++)
    {
        int u=gr[x][i];
        if(vis[u]==false&&edg[u][x]==false)dfs(u);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    while(k--)
    {
        v.clear();
        for(int i=0;i<n;i++)
        {
            int x;
            cin>>x;
            v.push_back(x);
        }
        for(int i=0;i<n-1;i++)
        {
            int x=v[i];
            for(int j=i+1;j<n;j++)
            {
                int xx=v[j];
                gr[x].push_back(xx);
                edg[x][xx]=true;
            }
        }
    }
    for(int i=0;i<n;i++)
    {
        dfs(v[i]);
        mx=max(ans,mx);
        memset(vis,0,sizeof vis);
        ans=0;
    }
    /*for(int i=1;i<=n;i++)
    {
        cout<<' '<<i<<endl;
        for(int j=0;j<gr[i].size();j++)cout<<gr[i][j]<<' ';
        cout<<endl;
    }*/
    cout<<mx;
}


#include<bits/stdc++.h>
using namespace std;
const long long md=1e9+7;
int n,k,ans,mx,edg[1010][1010],dis[1001];
vector<int>v,gr[100000];
bool vis[1001];
void dfs(int x,int l)
{
    vis[x]=true;
    dis[x]=0;
    for(int i=0; i<gr[x].size(); i++)
    {
        int u=gr[x][i];
        if(vis[u]==false)
        {
            dfs(u,l+1);

        }
        dis[x]=max(dis[x],dis[u]+1);
        mx=max(mx,dis[x]);
    }
}
int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);
    cin>>n>>k;
    int kk=k;
    while(k--)
    {
        for(int i=1; i<=n; i++)
        {
            int x;
            cin>>x;
            edg[x][k]=i;
        }
    }
    for(int i=1; i<=n; i++)
    {
        for(int j=1; j<=n; j++)
        {
            if(i==j)continue;
            bool t=false;
            for(int z=0;z<kk;z++)
            {
                if(edg[i][z]>edg[j][z])t=true;
            }
            if(t==false)gr[i].push_back(j);
        }
    }
    for(int i=1; i<=n; i++)
    {
        dfs(i,1);
        memset(vis,0,sizeof vis);
    }
    cout<<mx+1;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define inf INT_MAX
#define mini INT_MIN
#define MOD 100000000
#define all(v) (v).begin(),(v).end()
#define pii pair<int ,int >
#define pll pair<long long int ,long long int>
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
int arr[5][1005];
bool vis[1005];
//vector<int >v;
vector<int >v[1005];
//set<int >st;
//set<int >::iterator it;
//map<int ,int >mp;
//map<int ,int >::iterator it;
//priority_queue<int, vector<int >, greater<int > >pq;
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int ans=-1;
int dfs(int u,int dis)
{
    ans=max(ans,dis);
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i],dis+1);
        else
            ans=max(ans,dis+1);
    }
}
int main()
{
 int n,k,x,y;
 inp2(n,k);
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
      inp(arr[i][j]);
 mset(vis,false);
 bool flag=true;
 for(int i=1;i<=n;i++)
 {
    for(int j=i+1;j<=n;j++)
    {
        flag=true;
        for(int z=2;z<=k;z++)
        {
            for(int w=1;w<=n;w++)
            {
                if(arr[z][w]==arr[1][i])
                   x=w;
                else if(arr[z][w]==arr[1][j])
                   y=w;
            }
            if(x>y)
            {
                flag=false;
                break;
            }
        }
       if(flag)
                v[arr[1][i]].pb(arr[1][j]);
    }
 }
 for(int i=1;i<=n;i++)
 {
     if(!vis[i])
        {
            mset(vis,false);
            dfs(i,0);
        }
 }
 cout<<ans+1;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define inf INT_MAX
#define mini INT_MIN
#define MOD 100000000
#define all(v) (v).begin(),(v).end()
#define pii pair<int ,int >
#define pll pair<long long int ,long long int>
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
int arr[5][1005],dp[1005];
bool vis[1005];
//vector<int >v;
vector<int >v[1005];
//set<int >st;
//set<int >::iterator it;
//map<int ,int >mp;
//map<int ,int >::iterator it;
//priority_queue<int, vector<int >, greater<int > >pq;
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int ans=-1;
int dfs(int u)
{
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        dp[u]=max(dp[v[u][i]],dp[u]);
    }
    dp[u]++;
    ans=max(ans,dp[u]);
}
int main()
{
 int n,k,x,y;
 inp2(n,k);
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
      inp(arr[i][j]);
 mset(vis,false);
 bool flag=true;
 for(int i=1;i<=n;i++)
 {
    for(int j=i+1;j<=n;j++)
    {
        flag=true;
        for(int z=2;z<=k;z++)
        {
            for(int w=1;w<=n;w++)
            {
                if(arr[z][w]==arr[1][i])
                   x=w;
                else if(arr[z][w]==arr[1][j])
                   y=w;
            }
            if(x>y)
            {
                flag=false;
                break;
            }
        }
       if(flag)
                v[arr[1][i]].pb(arr[1][j]);
    }
 }
 mset(dp,0);
 for(int i=1;i<=n;i++)
 {
    mset(vis,false);
    mset(dp,0);
    dfs(i);
 }
 cout<<ans;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define inf INT_MAX
#define mini INT_MIN
#define MOD 100000000
#define all(v) (v).begin(),(v).end()
#define pii pair<int ,int >
#define pll pair<long long int ,long long int>
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
int arr[10][1005],dp[1005];
bool vis[1005];
//vector<int >v;
vector<int >v[1005];
//set<int >st;
//set<int >::iterator it;
//map<int ,int >mp;
//map<int ,int >::iterator it;
//priority_queue<int, vector<int >, greater<int > >pq;
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int ans=-1;
int dfs(int u)
{
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        dp[u]=max(dp[v[u][i]],dp[u]);
    }
    dp[u]++;
    ans=max(ans,dp[u]);
}
int main()
{
 int n,k,x,y;
 inp2(n,k);
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
      inp(arr[i][j]);
 mset(vis,false);
 bool flag=true;
 for(int i=1;i<=n;i++)
 {
    for(int j=i+1;j<=n;j++)
    {
        flag=true;
        for(int z=2;z<=k;z++)
        {
            for(int w=1;w<=n;w++)
            {
                if(arr[z][w]==arr[1][i])
                   x=w;
                else if(arr[z][w]==arr[1][j])
                   y=w;
            }
            if(x>y)
            {
                flag=false;
                break;
            }
        }
       if(flag)
                v[arr[1][i]].pb(arr[1][j]);
    }
 }
 mset(dp,0);
 for(int i=1;i<=n;i++)
 {
    mset(vis,false);
    mset(dp,0);
    dfs(i);
 }
 cout<<ans;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define inf INT_MAX
#define mini INT_MIN
#define MOD 100000000
#define all(v) (v).begin(),(v).end()
#define pii pair<int ,int >
#define pll pair<long long int ,long long int>
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
int arr[5][1005];
bool vis[1005];
//vector<int >v;
vector<int >v[1005];
//set<int >st;
//set<int >::iterator it;
//map<int ,int >mp;
//map<int ,int >::iterator it;
//priority_queue<int, vector<int >, greater<int > >pq;
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int ans=-1;
int dfs(int u,int dis)
{
    ans=max(ans,dis);
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i],dis+1);
    }
}
int main()
{
 int n,k,x,y;
 inp2(n,k);
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
      inp(arr[i][j]);
 mset(vis,false);
 bool flag=true;
 for(int i=1;i<=n;i++)
 {
    for(int j=i+1;j<=n;j++)
    {
        flag=true;
        for(int z=2;z<=k;z++)
        {
            for(int w=1;w<=n;w++)
            {
                if(arr[z][w]==arr[1][i])
                   x=w;
                else if(arr[z][w]==arr[1][j])
                   y=w;
            }
            if(x>y)
            {
                flag=false;
                break;
            }
        }
       if(flag)
                v[arr[1][i]].pb(arr[1][j]);
    }
 }
 for(int i=1;i<=n;i++)
 {
     if(!vis[i])
        dfs(i,0);
 }
 cout<<ans+1;
}


#include<bits/stdc++.h>
#define ull unsigned long long
#define ll long long
#define pb push_back
#define F first
#define S second
#define mk make_pair
#define inf INT_MAX
#define mini INT_MIN
#define MOD 100000000
#define all(v) (v).begin(),(v).end()
#define pii pair<int ,int >
#define pll pair<long long int ,long long int>
#define mset(v,x) memset(v,x,sizeof(v))
#define MODSET(d) if ((d) >= MOD) d %= MOD;
#define inp(x) (scanf("%d",&x))
#define inp2(x,y) (scanf("%d%d",&x,&y))
#define inp3(x,y,z) (scanf("%d%d%d",&x,&y,&z))
#define inp4(x,y,z,w) (scanf("%d%d%d%d",&x,&y,&z,&w))
#define inpl(d) scanf("%lld",&d)
#define inpl2(a,b) scanf("%lld%lld",&a,&b)
#define inpl3(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
#define inpl4(a,b,c,d) scanf("%lld%lld%lld%lld",&a,&b,&c,&d)
using namespace std;
int arr[10][1005],dp[1005],pos[10][1005];
bool vis[1005];
//vector<int >v;
vector<int >v[1005];
//set<int >st;
//set<int >::iterator it;
//map<int ,int >mp;
//map<int ,int >::iterator it;
//priority_queue<int, vector<int >, greater<int > >pq;
/********************************************* YOUR CODE GOES FROM HERE ************************************************/
int ans=-1;
int dfs(int u)
{
    vis[u]=true;
    for(int i=0;i<v[u].size();i++)
    {
        if(!vis[v[u][i]])
            dfs(v[u][i]);
        dp[u]=max(dp[v[u][i]],dp[u]);
    }
    dp[u]++;
    ans=max(ans,dp[u]);
}
int main()
{
 int n,k,x,y;
 inp2(n,k);
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
      inp(arr[i][j]);
 mset(vis,false);
 bool flag=true;
 for(int i=1;i<=k;i++)
    for(int j=1;j<=n;j++)
        pos[i][arr[i][j]]=j;
 for(int i=1;i<=n;i++)
 {
    for(int j=i+1;j<=n;j++)
    {
        flag=true;
        for(int z=2;z<=k;z++)
        {
            x=pos[z][arr[1][i]];
            y=pos[z][arr[1][j]];
            if(x>y)
            {
                flag=false;
                break;
            }
        }
       if(flag)
                v[arr[1][i]].pb(arr[1][j]);
    }
 }
 mset(dp,0);
 for(int i=1;i<=n;i++)
 {
    mset(vis,false);
    mset(dp,0);
    dfs(i);
 }
 cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e3+5;

vector<int>g[N];
int used[N],pos[6][N];
int ans=0;

void dfs(int v,int lvl){
    used[v]=1;
    ans=max(ans,lvl);
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to,lvl+1);
    }
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            int a;
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            int w=0;
            for(int k=1;k<=m;k++){
                if(pos[k][i]>pos[k][j]){
                    w=1;
                    break;
                }
            }
            if(!w)g[i].pb(j);
        }
    }
    for(int i=1;i<=n;i++){
        dfs(i,1);
        for(int j=1;j<=n;j++){
            used[j]=0;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e3+5;

vector<int>g[N];
int used[N],pos[6][N],dp[N];
int ans=0;

void dfs(int v){
    used[v]=1;
    dp[v]=1;
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to);
        dp[v]=max(dp[v],dp[to]+1);
        ans=max(ans,dp[v]);
    }
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            int a;
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            int w=0;
            for(int k=1;k<=m;k++){
                if(pos[k][i]>pos[k][j]){
                    w=1;
                    break;
                }
            }
            if(!w)g[i].pb(j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!used[i])dfs(i);
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e5+5;

vector<int>gr[N],g[N];
int d[N],dp[N],used[N],a[N];

void dfs(int v){
    used[v]=1;
    int mx=1;
    for(int i=0;i<sz(gr[v]);i++){
        int to=gr[v][i];
        mx=max(mx,dp[to]+1);
    }
    dp[v]=min(dp[v],mx);
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to);
    }
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        dp[i]=inf;
    }
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            cin>>a[j];
        }
        for(int j=1;j<=n;j++){
            for(int k=j+1;k<=n;k++){
                g[a[j]].pb(a[k]);
                gr[a[k]].pb(a[j]);
            }
        }
        dfs(a[1]);
        for(int j=1;j<=n;j++){
            used[j]=0;
            g[j].clear();
            gr[j].clear();
        }
    }
    int ans=0;
    for(int i=1;i<=n;i++){
        ans=max(ans,dp[i]);
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e3+5;

vector<int>g[N];
int used[N],pos[6][N];
int ans=0;

void dfs(int v,int lvl){
    used[v]=1;
    ans=max(ans,lvl);
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to,lvl+1);
    }
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            int a;
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            int w=0;
            for(int k=1;k<=m;k++){
                if(pos[k][i]>pos[k][j]){
                    w=1;
                    break;
                }
            }
            if(!w)g[i].pb(j);
        }
    }
    for(int i=1;i<=n;i++){
        dfs(i,1);
        for(int j=1;j<=n;j++){
            used[j]=0;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e5+5;

vector<int>g[N];
int used[N],mn[N];
int ans=0;

void dfs(int v,int lvl){
    used[v]=1;
    ans=max(ans,lvl);
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to,lvl+1);
    }
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=n;i++){
        mn[i]=inf;
    }
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            int a;
            cin>>a;
            mn[a]=min(mn[a],j);
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(mn[i]>mn[j]){
                g[i].pb(j);
            }
        }
    }
    for(int i=1;i<=n;i++){
        dfs(i,1);
        for(int j=1;j<=n;j++){
            used[j]=0;
        }
    }
    cout<<ans;
}


#include <bits/stdc++.h>
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define up_b upper_bound
#define low_b lower_bound
#define sz(x) (int)x.size()
#define all(v) v.begin(),v.end()
#define boost ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0)

using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<int,ll> pil;
typedef pair<ll,int> pli;
typedef pair<ll,ll> pll;

const ll INF=2e18;
const int inf = 2e9;
const ll mod = 1e8;
const int dx[4]={0,0,1,-1};
const int dy[4]={1,-1,0,0};
const int M=40;
void add(int &a,int b){
	a+=b;
	if(a>=mod)a-=mod;
}
const int N=1e3+5;

vector<int>g[N];
int used[N],pos[6][N],dp[N];
int ans=0;

void dfs(int v){
    used[v]=1;
    dp[v]=1;
    for(int i=0;i<sz(g[v]);i++){
        int to=g[v][i];
        if(!used[to])dfs(to);
        dp[v]=max(dp[v],dp[to]+1);
    }
    ans=max(ans,dp[v]);
}

int main(){
    boost;
    int n,m;
    cin>>n>>m;
    for(int i=1;i<=m;i++){
        for(int j=1;j<=n;j++){
            int a;
            cin>>a;
            pos[i][a]=j;
        }
    }
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            if(i==j)continue;
            int w=0;
            for(int k=1;k<=m;k++){
                if(pos[k][i]>pos[k][j]){
                    w=1;
                    break;
                }
            }
            if(!w)g[i].pb(j);
        }
    }
    for(int i=1;i<=n;i++){
        if(!used[i])dfs(i);
    }
    cout<<ans;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define read freopen("in.in","r",stdin)
#define write freopen("out.out","w",stdout)
#define FastIO ios::sync_with_stdio(0)
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
const int N = 1003;
int memo[N][N];
string grid[N];
string s1, s2;
int dp(int i, int j) {
	if (i < 0 || j < 0)
		return 0;
	int &ret = memo[i][j];
	if (ret != -1)return ret;

	if (s1[i] == s2[j])
		ret = dp(i - 1, j - 1) + 1;
	else ret = max(dp(i, j - 1), dp(i - 1, j));
	return ret;
}
int main() {
	FastIO;
#ifndef ONLINE_JUDGE
	read; write;
#endif
	int n, k; cin >> n >> k;
	char x;
	for (int i = 0; i < k; i++)
		for (int j = 0; j < n; j++) {
			cin >> x;
			grid[i].push_back(x);
		}
	int ans = 100000;
	for (int i = 0; i < k; i++) {
		for (int j = i + 1; j < k; j++) {
			s1 = grid[i];
			s2 = grid[j];
			memset(memo, -1, sizeof memo);
			ans = min(ans, dp(s1.size() - 1, s2.size() - 1));
		}
	}
	cout << ans << endl;

	return 0;
}


#define _CRT_SECURE_NO_WARNINGS
#include<bits/stdc++.h>
using namespace std;
#define read freopen("in.in","r",stdin)
#define write freopen("out.out","w",stdout)
#define FastIO ios::sync_with_stdio(0)
#define mod 1000000007
typedef long long ll;
typedef vector<int> vi;
typedef pair<int, int> ii;
const int N = 1003;
int vis[N],dis[N],grid[10][N], pos[10][N];
vector<vi>adjlist;
set<ii>edges;
vi topsort;
int n;
void dfs(int node) {
	if (vis[node])return;
	vis[node] = 1;
	for (int i = 0; i < adjlist[node].size(); i++)
	{
		int child = adjlist[node][i];
		if (!vis[child])
			dfs(child);
	}
	topsort.push_back(node);
}
int shorttestbath() {
	memset(vis, 0, sizeof vis);
	memset(dis, INT_MAX, sizeof dis);
	for (int i = 0; i < topsort.size(); i++) {
		int node = topsort[i];

		for (int j=0;j<adjlist[node].size();j++)
		{
			int child = adjlist[node][j];

			if (!vis[child])
				dis[child] = min(dis[child] , dis[node] - 1);
		}
	}
	int ans = INT_MAX;
	for (int i = 0; i <=n; i++)
		ans = min(ans, dis[i]);
	return -ans;
}
int main() {
	FastIO;
#ifndef ONLINE_JUDGE
	read; write;
#endif
	int m; cin >> n >> m;
	int a, b;
	adjlist.resize(n+1);
	for (int i = 0; i < m; i++)
		for (int j = 0; j < n; j++) {
			cin >> grid[i][j];
			pos[i][grid[i][j]] = j;
		}
	for (int i = 1; i <=n; i++)
		for (int j = 1; j <=n; j++)
		{
			if (i == j)continue;
			bool can = true;
			for (int k = 0; k < m; k++)
				if (pos[k][i] > pos[k][j])can = false;
			if (can)adjlist[i].push_back(j);
		}

	for (int i = 1; i <= n; i++)
		dfs(i);
	reverse(topsort.begin(), topsort.end());
	int ans = shorttestbath();
	cout << ans << endl;
	return 0;
}