#include <bits/stdc++.h>

#define FOR(i,a,b) for( ll i = (a); i < (ll)(b); i++ )
#define REP(i,n) FOR(i,0,n)
#define YYS(x,arr) for(auto& x:arr)
#define ALL(x) (x).begin(),(x).end()
#define SORT(x) sort( (x).begin(),(x).end() )
#define REVERSE(x) reverse( (x).begin(),(x).end() )
#define UNIQUE(x) (x).erase( unique( ALL( (x) ) ) , (x).end() )
#define PW(x) (1LL<<(x))
#define SZ(x) ((ll)(x).size())
#define pb emplace_back
#define fi first
#define se second

using namespace std;

typedef long double ld;
typedef long long int ll;
typedef pair<int,int> pi;
typedef pair<ll,ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<vpl> gr;
typedef vector<vl> ml;
typedef vector<vd> md;
typedef vector<vi> mi;

const ll INF = (ll)1e9 + 10;
const ll INFLL = (ll)1e18 + 10;
const ld EPS = 1e-8;
const ld EPSLD = 1e-18;
const ll MOD = 1e9+7;

template<class T> T &chmin( T &a , const T &b ){ return a = min(a,b); }
template<class T> T &chmax( T &a , const T &b ){ return a = max(a,b); }

const ll MAX_N = 100010;

ll n, k, w;
char s[MAX_N];

ll sum[12][MAX_N];
ll all;

int main(){

  scanf( "%lld %lld %lld" , &n , &k , &w );
  scanf( "%s" , s );

  REP( i , n ) sum[i%k][i/k+1] = sum[i%k][i/k] + ( s[i] - '0' );
  REP( i , n ) all += s[i] - '0';

  REP( i , w ){
    ll l, r;
    scanf( "%lld %lld" , &l , &r ); l--; r--;
    ll ans = 0;
    REP( j , k-1 ) ans += sum[(r-k+1+j)%k][(r-k+1+j)/k+1] - sum[(l+j)%k][(l+j)/k];
    ans += (r-l+1)/k - ( sum[r%k][r/k+1] - sum[(l+k-1)%k][(l+k-1)/k] );
    printf( "%lld\n" , ans );
  }

  return 0;
}


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

int arr[1010][5];
vector<int> adj[1010];
int ans[1010];
int best = -1;
int visited[1010];

void dfs(int node)
{
	visited[node] = 1;

	for(int i=0;i<adj[node].size();i++)
	{
		if(!visited[adj[node][i]])
		dfs(adj[node][i]);

		ans[node] = max(ans[node],ans[adj[node][i]]+1);
	}

	ans[node] = max(1,ans[node]);

	best = max(best,ans[node]);
}

int main()
{
	int n,k;
	cin>>n>>k;

	for(int i=0;i<k;i++)
	{
		for(int j=0;j<n;j++)
		{
			int x;
			cin>>x;

			arr[i][x] = j+1;
		}
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int flag = 0;

			for(int k1=0;k1<k;k1++)
			{
				if(arr[k1][i]>arr[k1][j] && i!=j)
				flag = 1;
			}

			if(!flag && i!=j)
			{
				//cout<<"i "<<i<<" j "<<j<<endl;
				adj[i].push_back(j);
			}
		}
	}

	for(int i=1;i<=n;i++)
	if(!visited[i])
	{
		dfs(i);
	}

	cout<<best<<endl;
}


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

int arr[1010][5];
vector<int> adj[1010];
int ans[1010];
int best = -1;
int visited[1010];

void dfs(int node)
{
	visited[node] = 1;

	for(int i=0;i<adj[node].size();i++)
	{
		if(!visited[adj[node][i]])
		dfs(adj[node][i]);

		ans[node] = max(ans[node],ans[adj[node][i]]+1);
	}

	ans[node] = max(1,ans[node]);

	best = max(best,ans[node]);
}

int main()
{
	int n,k;
	cin>>n>>k;

	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int x;
			cin>>x;

			arr[i][x] = j;
		}
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==j)
			continue;

			int flag = 0;

			for(int k1=1;k1<=k;k1++)
			{
				if(arr[k1][i]>=arr[k1][j])
				flag = 1;
			}

			if(!flag)
			{
				//cout<<"i "<<i<<" j "<<j<<endl;
				adj[i].push_back(j);
			}
		}
	}

	for(int i=1;i<=n;i++)
	if(!visited[i])
	{
		dfs(i);
	}

	cout<<best<<endl;
}


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

int arr[1010][5];
vector<int> adj[1010];
int ans[1010];
int best = -1;
int visited[1010];

void dfs(int node)
{
	visited[node] = 1;

	for(int i=0;i<adj[node].size();i++)
	{
		if(!visited[adj[node][i]])
		dfs(adj[node][i]);

		ans[node] = max(ans[node],ans[adj[node][i]]+1);
	}

	ans[node] = max(1,ans[node]);

	best = max(best,ans[node]);
}

int main()
{
	int n,k;
	cin>>n>>k;

	for(int i=0;i<k;i++)
	{
		for(int j=0;j<n;j++)
		{
			int x;
			cin>>x;

			arr[i][x] = j+1;
		}
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==j)
			continue;

			int flag = 0;

			for(int k1=0;k1<k;k1++)
			{
				if(arr[k1][i]>=arr[k1][j])
				flag = 1;
			}

			if(!flag)
			{
				//cout<<"i "<<i<<" j "<<j<<endl;
				adj[i].push_back(j);
			}
		}
	}

	for(int i=1;i<=n;i++)
	if(!visited[i])
	{
		dfs(i);
	}

	cout<<best<<endl;
}


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

int arr[1010][6];
vector<int> adj[1010];
int ans[1010];
int best = 0;
int visited[1010];

void dfs(int Node)
{
    visited[Node] = 1;

    for(int i = 0; i < adj[Node].size(); ++ i)
    {
        if(!visited[adj[Node][i]])
            dfs(adj[Node][i]);
        ans[Node] = max(ans[Node], ans[adj[Node][i]] + 1);
    }
    ans[Node] = max(ans[Node], 1);
    best = max(best, ans[Node]);
}

int main()
{
    int n,k;
    cin>>n>>k;

    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;

            arr[i][x] = j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            continue;

            int flag = 0;

            for(int k1=1;k1<=k;k1++)
            {
                if(arr[k1][i]>=arr[k1][j])
                flag = 1;
            }

            if(!flag)
            {
                //cout<<"i "<<i<<" j "<<j<<endl;
                adj[j].push_back(i);
            }
        }
    }

    for(int i=1;i<=n;i++)
    if(!visited[i])
    {
        dfs(i);
    }

    cout<<best<<endl;
}


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

int arr[6][1010];
vector<int> adj[1010];
int ans[1010];
int best = 0;
int visited[1010];

void dfs(int Node)
{
    visited[Node] = 1;

    for(int i = 0; i < adj[Node].size(); ++ i)
    {
        if(!visited[adj[Node][i]])
            dfs(adj[Node][i]);
        ans[Node] = max(ans[Node], ans[adj[Node][i]] + 1);
    }
    ans[Node] = max(ans[Node], 1);
    best = max(best, ans[Node]);
}

int main()
{
    int n,k;
    cin>>n>>k;

    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;

            arr[i][x] = j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            continue;

            int flag = 0;

            for(int k1=1;k1<=k;k1++)
            {
                if(arr[k1][i]>=arr[k1][j])
                flag = 1;
            }

            if(!flag)
            {
                //cout<<"i "<<i<<" j "<<j<<endl;
                adj[j].push_back(i);
            }
        }
    }

    for(int i=1;i<=n;i++)
    if(!visited[i])
    {
        dfs(i);
    }

    cout<<best<<endl;
}


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

int arr[1010][5];
vector<int> adj[1010];
int ans[1010];
int best = 0;
int visited[1010];

void dfs(int node)
{
	visited[node] = 1;

	for(int i=0;i<adj[node].size();i++)
	{
		if(!visited[adj[node][i]])
		dfs(adj[node][i]);

		ans[node] = max(ans[node],ans[adj[node][i]]+1);
	}

	ans[node] = max(1,ans[node]);

	best = max(best,ans[node]);
}

int main()
{
	int n,k;
	cin>>n>>k;

	for(int i=1;i<=k;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int x;
			cin>>x;

			arr[i][x] = j;
		}
	}

	for(int i=1;i<=n;i++)
	{
		for(int j=1;j<=n;j++)
		{
			if(i==j)
			continue;

			int flag = 0;

			for(int k1=1;k1<=k;k1++)
			{
				if(arr[k1][i]>=arr[k1][j])
				flag = 1;
			}

			if(!flag)
			{
				//cout<<"i "<<i<<" j "<<j<<endl;
				adj[i].push_back(j);
			}
		}
	}

	for(int i=1;i<=n;i++)
	if(!visited[i])
	{
		dfs(i);
	}

	cout<<best<<endl;
}


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

int arr[1010][6];
vector<int> adj[1010];
int ans[1010];
int best = 0;
int visited[1010];

void dfs(int Node)
{
    visited[Node] = 1;

    for(int i = 0; i < adj[Node].size(); ++ i)
    {
        if(!visited[adj[Node][i]])
            dfs(adj[Node][i]);
        ans[Node] = max(ans[Node], ans[adj[Node][i]] + 1);
    }
    ans[Node] = max(ans[Node], 1);
    best = max(best, ans[Node]);
}

int main()
{
    int n,k;
    cin>>n>>k;

    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;

            arr[i][x] = j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            continue;

            int flag = 0;

            for(int k1=1;k1<=k;k1++)
            {
                if(arr[k1][i]>=arr[k1][j])
                flag = 1;
            }

            if(!flag)
            {
                //cout<<"i "<<i<<" j "<<j<<endl;
                adj[i].push_back(j);
            }
        }
    }

    for(int i=1;i<=n;i++)
    if(!visited[i])
    {
        dfs(i);
    }

    cout<<best<<endl;
}


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

int arr[1010][6];
vector<int> adj[1010];
int ans[1010];
int best = 0;
int visited[1010];

void dfs(int node)
{
    visited[node] = 1;

    for(int i=0;i<adj[node].size();i++)
    {
        if(!visited[adj[node][i]])
        dfs(adj[node][i]);

        ans[node] = max(ans[node],ans[adj[node][i]]+1);
    }

    ans[node] = max(1,ans[node]);

    best = max(best,ans[node]);
}

int main()
{
    int n,k;
    cin>>n>>k;

    for(int i=1;i<=k;i++)
    {
        for(int j=1;j<=n;j++)
        {
            int x;
            cin>>x;

            arr[i][x] = j;
        }
    }

    for(int i=1;i<=n;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(i==j)
            continue;

            int flag = 0;

            for(int k1=1;k1<=k;k1++)
            {
                if(arr[k1][i]>=arr[k1][j])
                flag = 1;
            }

            if(!flag)
            {
                //cout<<"i "<<i<<" j "<<j<<endl;
                adj[i].push_back(j);
            }
        }
    }

    for(int i=1;i<=n;i++)
    if(!visited[i])
    {
        dfs(i);
    }

    cout<<best<<endl;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
vector < int > v[mx] ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max ( max1 , d ) ;
    for ( int i=0 ; i<v[x].size() ; i++ )
        if ( !mark1[v[x][i]] )
            dfs ( v[x][i] , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=i+1 ; j<=n ; j++ )
            if ( mark[a[i]][a[j]]==k )
                v[a[i]].pb( a[j] ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[a[i]] )
            dfs ( a[i] , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
vector < int > v[mx] ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max ( max1 , d ) ;
    for ( int i=0 ; i<v[x].size() ; i++ )
        if ( !mark1[v[x][i]] )
            dfs ( v[x][i] , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=i+1 ; j<=n ; j++ )
            if ( mark[a[i]][a[j]]==k )
                v[a[i]].pb( a[j] ) ;
    for ( int i=1 ; i<=n ; i++ )
            dfs ( a[i] , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
vector < int > v[mx] ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max ( max1 , d ) ;
    for ( int i=0 ; i<v[x].size() ; i++ )
        if ( !mark1[v[x][i]] )
            dfs ( v[x][i] , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=i+1 ; j<=n ; j++ )
            if ( mark[i][j]==k )
                v[i].pb( j ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[i] )
            dfs ( i , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
vector < int > v[mx] ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max ( max1 , d ) ;
    for ( int i=0 ; i<v[x].size() ; i++ )
            dfs ( v[x][i] , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=i+1 ; j<=n ; j++ )
            if ( mark[a[i]][a[j]]==k )
                v[a[i]].pb( a[j] ) ;
    for ( int i=1 ; i<=n ; i++ )
            dfs ( a[i] , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max( d , max1 ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( mark[x][i]==k )
            dfs( i , d+1 ) ;
}
int main()
{
    ios::sync_with_stdio( false ) ;
    cin >> n >> k ;
    int k1=k ;
    while ( k1-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            cin >> a[i] ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[i] )
            dfs ( i , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
vector < int > v[mx] ;
int mark[mx][mx] , a[mx] , n , k , max1 , dp[mx] ;
bool mark1[mx] ;

void dfs ( int x )
{
    mark1[x]=1 ;
    for ( int i=0 ; i<v[x].size() ; i++ )
    {
        if ( dp[v[x][i]]==-1 )
            dfs ( v[x][i] ) ;
        dp[x]=max( dp[x] , dp[v[x][i]]+1 ) ;
    }
    max1=max( max1 , dp[x] ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        for ( int j=i+1 ; j<=n ; j++ )
            if ( mark[a[i]][a[j]]==k )
                v[a[i]].pb( a[j] ) ;
    memset ( dp , -1 , sizeof dp ) ;
    for ( int i=1 ; i<=n ; i++ )
    {
        if ( v[i].size()==0 )
            dp[i]=1 ;
    }
    for ( int i=1 ; i<=n ; i++ )
            dfs ( i ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max( d , max1 ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( mark[x][i]==k )
            dfs( i , d+1 ) ;
}
int main()
{
    ios::sync_with_stdio( false ) ;
    cin >> n >> k ;
    int k1=k ;
    while ( k1-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            cin >> a[i] ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[a[i]] )
            dfs ( a[i] , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d , int j )
{
    mark1[x]=1 ;
    max1=max( d , max1 ) ;
    for ( int i=j ; i<=n ; i++ )
        if ( mark[x][a[i]]==k )
            dfs( a[i] , d+1 , i+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[a[i]] )
            dfs ( a[i] , 1 , i+1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;

void dfs ( int x , int d )
{
    max1=max( d , max1 ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( mark[x][i]==k )
            dfs( i , d+1 ) ;
}
int main()
{
    ios::sync_with_stdio( false ) ;
    cin >> n >> k ;
    int k1=k ;
    while ( k1-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            cin >> a[i] ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        dfs ( i , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;

void dfs ( int x , int d )
{
    max1=max( d , max1 ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( mark[x][i]==k )
            dfs( i , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    int k1=k ;
    while ( k1-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            cin >> a[i] ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        dfs ( i , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d , int j )
{
    mark1[x]=1 ;
    max1=max( d , max1 ) ;
    for ( int i=j ; i<=n ; i++ )
        if ( mark[x][a[i]]==k && !mark1[a[i]] )
            dfs( a[i] , d+1 , i+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[a[i]] )
            dfs ( a[i] , 1 , i+1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std ;

const int mx=1e3+10 ;
#define pb push_back
#define F first
#define S second
typedef pair < int , int > pii ;
int mark[mx][mx] , a[mx] , n , k , max1 ;
bool mark1[mx] ;

void dfs ( int x , int d )
{
    mark1[x]=1 ;
    max1=max( d , max1 ) ;
    for ( int i=1 ; i<=n ; i++ )
        if ( mark[x][i]==k && !mark1[i] )
            dfs( i , d+1 ) ;
}
int main()
{
    cin >> n >> k ;
    for ( int p=k ; p>0 ; p-- )
    {
        for ( int i=1 ; i<=n ; i++ )
            scanf( "%d" ,&a[i] ) ;
        for ( int i=1 ; i<=n ; i++ )
            for ( int j=i+1 ; j<=n ; j++ )
                mark[a[i]][a[j]]++ ;
    }
    for ( int i=1 ; i<=n ; i++ )
        if ( !mark1[a[i]] )
            dfs ( a[i] , 1 ) ;
    cout << max1 ;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx, dp[N];

int dfs(int node, int stp) {
	if (dp[node]) return dp[node] + stp;
	dp[node] = stp;
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
	hash2[node] = false;
	return stp;
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx, dp[N];

int dfs(int node, int stp) {
	if (dp[node]) return dp[node];
	dp[node] = stp;
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
	hash2[node] = false;
	return stp;
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx, dp[N];

int dfs(int node) {
	if (dp[node] != -1) return dp[node];
	int mx = 1;
	for (int i = 0; i < gr[node].size(); i++)
		mx = max(mx, dfs(gr[node][i]) + 1);
	dp[node] = mx;
	return mx;
}

int main() {

	scanf("%d %d", &n, &k);

	memset(dp, -1, sizeof dp);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++)
		mx = max(dfs(i), mx);

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx;

void dfs(int node, int stp) {
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
	hash2[node] = false;
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx;

void dfs(int node, int stp) {
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = i+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx + 1);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx;

void dfs(int node, int stp) {
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx;

void dfs(int node, int stp) {
	hash2[node] = true;
	mx = max(mx, stp);
	for (int i = 0; i < gr[node].size(); i++)
		if (!hash2[gr[node][i]])
			dfs(gr[node][i], stp+1);
	hash2[node] = false;
}

int main() {

	scanf("%d %d", &n, &k);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = i+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++) {
		dfs(i, 1);
		memset(hash2, false, sizeof hash2);
	}

	printf("%d\n", mx + 1);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx, dp[N];

int dfs(int node, int stp) {
	if (dp[node] != -1) return dp[node];
	int mx = 1;
	for (int i = 0; i < gr[node].size(); i++)
		mx = max(mx, dfs(gr[node][i], stp+1) + 1);
	dp[node] = mx;
	return mx;
}

int main() {

	scanf("%d %d", &n, &k);

	memset(dp, -1, sizeof dp);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++)
		mx = max(dfs(i, 1), mx);

	printf("%d\n", mx);

	return 0;
}


#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair < int, int > pii;

const ll linf = 1e18 + 9;
const int inf = 1e9 + 9;
const int N = 1e3 + 9;

vector < int > gr[N];

int n, k, arr[7][N], hash[N][N], hash2[N], mx, dp[N];

int dfs(int node, int stp) {
	if (!gr[node].size()) return 0;
	if (dp[node] != -1) return dp[node];
	int mx = 0;
	for (int i = 0; i < gr[node].size(); i++)
		mx = max(mx, dfs(gr[node][i], stp+1) + 1);
	dp[node] = mx + stp;
	return mx;
}

int main() {

	scanf("%d %d", &n, &k);

	memset(dp, -1, sizeof dp);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			scanf("%d", &arr[i][j]);

	for (int i = 1; i <= k; i++)
		for (int j = 1; j <= n; j++)
			for (int t = j+1; t <= n; t++)
					hash[arr[i][j]][arr[i][t]]++;

	for (int i = 1; i <= n; i++)
		for (int j = 1; j <= n; j++)
			if (hash[i][j] == k)
				gr[i].push_back(j);

	for (int i = 1; i <= n; i++)
		mx = max(dfs(i, 0), mx);

	printf("%d\n", mx + 1);

	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<ctime>
using namespace std;

int n,k;
int a[1003];
int f[1003][1003];
vector<int> g[1003];
bool visit[1003];
int fa[1003];
int ans;

void dfs(int i,int s)
{
     if(ans<=s) ans=s;
     visit[i]=true;
     for(int j=0;j<g[i].size();j++)
     if(!visit[g[i][j]]) dfs(g[i][j],s+1);
     visit[i]=false;
}

int main()
{
    scanf("%d%d",&n,&k);
    memset(f,0,sizeof(f));
    for(int q=1;q<=k;q++)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<=i-1;j++)
            {
                f[a[j]][a[i]]++;
                if(f[a[j]][a[i]]==k)
                {
                    g[a[j]].push_back(a[i]);
                    fa[a[i]]=2;
                    if(fa[a[j]]==0) fa[a[j]]=1;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==1)
        {
            memset(visit,false,sizeof(visit));
            dfs(i,1);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<cctype>
#include<string>
#include<string.h>
#include<list>
using namespace std;

int n,m;
int a[10][1050];
int f[1050];
vector<int> g[1050];
int ans;
queue<int> q;

bool check(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(a[i][x]>a[i][y]) return false;
	}
	return true;
}

void init()
{
	for(int i=1;i<=n;i++)
	{
		g[0].push_back(i);
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(check(i,j)) g[i].push_back(j);
		}
	}
}

void bfs()
{
	q.push(0);
	while(!q.empty())
	{
		int x=q.front();q.pop();
		if(ans<f[x]) ans=f[x];
		for(int i=0;i<g[x].size();i++)
		{
			int y=g[x][i];
			if(f[x]+1>f[y])
			{
				f[y]=f[x]+1;
				q.push(y);
			}
		}
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int x;
			scanf("%d",&x);
			a[i][x]=j;
		}
	}

	init();
	bfs();
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<utility>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<cmath>
#include<cstring>
#include<cctype>
#include<string>
#include<string.h>
#include<list>
using namespace std;

typedef pair<int,int> P;
int n,m;
int a[10][1050];
int f[1050];
vector<int> g[1050];
int ans;
queue<P> q;

bool check(int x,int y)
{
	for(int i=1;i<=m;i++)
	{
		if(a[i][x]>a[i][y]) return false;
	}
	return true;
}

void init()
{
	for(int i=1;i<=n;i++)
	{
		g[0].push_back(i);
		for(int j=1;j<=n;j++)
		{
			if(i==j) continue;
			if(check(i,j)) g[i].push_back(j);
		}
	}
}

void bfs()
{
	q.push(P(0,0));
	while(!q.empty())
	{
		P x=q.front();q.pop();
		//x.first num     x.second sum
		if(f[x.first]>x.second) continue;
		f[x.first]=x.second;
	//	cout<<x.first<<" "<<x.second<<endl;
		if(ans<x.second) ans=x.second;
		for(int i=0;i<g[x.first].size();i++)
		{
			//g[x][i]
			q.push(P(g[x.first][i],x.second+1));
		}
	}
}

int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=m;i++)
	{
		for(int j=1;j<=n;j++)
		{
			int x;
			scanf("%d",&x);
			a[i][x]=j;
		}
	}

	init();
	bfs();
	printf("%d\n",ans);
	return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<ctime>
using namespace std;

int n,k;
int a[1003];
int f[1003][1003];
vector<int> g[1003];
bool visit[1003];
int fa[1003];
int ans;

void dfs(int i,int s)
{
     if(ans<=s) ans=s;
     visit[i]=true;
     for(int j=0;j<g[i].size();j++)
     if(!visit[g[i][j]]) dfs(g[i][j],s+1);
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int q=1;q<=k;q++)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<=i-1;j++)
            {
                f[a[j]][a[i]]++;
                if(f[a[j]][a[i]]==k)
                {
                    g[a[j]].push_back(a[i]);
                    fa[a[i]]=2;
                    if(fa[a[j]]==0) fa[a[j]]=1;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==1)
        {
            memset(visit,false,sizeof(visit));
            dfs(i,1);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<ctime>
using namespace std;

int n,k;
int a[1003];
int f[1003][1003];
vector<int> g[1003];
bool visit[1003];
int fa[1003];
int ans;

void dfs(int i,int s)
{
     if(ans<=s) ans=s;
     visit[i]=true;
     for(int j=0;j<g[i].size();j++)
     if(!visit[g[i][j]]) dfs(g[i][j],s+1);
}

int main()
{
    scanf("%d%d",&n,&k);
    for(int q=1;q<=k;q++)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<=i-1;j++)
            {
                f[a[j]][a[i]]++;
                if(f[a[j]][a[i]]==k)
                {
                    g[a[j]].push_back(a[i]);
                    //fa[a[i]]=2;
                    //if(fa[a[j]]==0) fa[a[j]]=1;
                }
            }
        }
    }
    for(int i=1;i<=n;i++)
    {
        //if(fa[i]==1)
        //{
            memset(visit,false,sizeof(visit));
            dfs(i,1);
        //}
    }
    printf("%d\n",ans);
    return 0;
}


#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<utility>
#include<vector>
#include<queue>
#include<set>
#include<map>
#include<ctime>
using namespace std;

int n,k;
int a[1003];
int f[1003][1003];
vector<int> g[1003];
bool visit[1003];
int fa[1003];
int ans;

void dfs(int i,int s)
{
     if(ans<=s) ans=s;
     visit[i]=true;
     for(int j=0;j<g[i].size();j++)
     if(!visit[g[i][j]]) dfs(g[i][j],s+1);
     visit[i]=false;
}

int main()
{
    scanf("%d%d",&n,&k);
    memset(f,0,sizeof(f));
    for(int q=1;q<=k;q++)
    {
        for(int i=1;i<=n;i++)
        {
            scanf("%d",&a[i]);
            for(int j=1;j<=i-1;j++)
            {
                f[a[j]][a[i]]++;
                if(f[a[j]][a[i]]==k)
                {
                    g[a[j]].push_back(a[i]);
                    fa[a[i]]=2;
                    if(fa[a[j]]==0) fa[a[j]]=1;
                }
            }
        }
    }
    ans=1;
    for(int i=1;i<=n;i++)
    {
        if(fa[i]==1)
        {
            memset(visit,false,sizeof(visit));
            dfs(i,1);
        }
    }
    printf("%d\n",ans);
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n);
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	for (int j=0; j<i; ++j)
		if (a[j] < a[i])
			d[i] = max (d[i], 1 + d[j]);
    }

    int ans = d[0];
    for (int i=0; i<n; ++i)
	ans = max (ans, d[i]);
    return ans;
}

int main(){
    int n,m,d,u=0,t=1;
    int a[1009];
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[j];
        d=mmm(a,n);
        //cout<<d<<endl;
        if(i==0)t=d;else
        t=min(t,d);
    }
    cout<<t;
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n); // ��������� MAXN ����� ����������� ���������� �������� n
    int p[1009]; // ��������� MAXN ����� ����������� ���������� �������� n
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	p[i] = -1;
	for (int j=0; j<i; ++j)
		if (a[j] < a[i])
			if (1 + d[j] > d[i]) {
				d[i] = 1 + d[j];
				p[i] = j;
			}
    }

    int ans = d[0],  pos = 0;
    for (int i=0; i<n; ++i)
	if (d[i] > ans) {
		ans = d[i];
		pos = i;
	}

    if(ans==1)return ans+2;else return ans;
}

int main(){
    int n,m,d,u=0;
    int a[10000];
    map<int,int>A;
    map<int,int>::iterator it;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[i];
        d=mmm(a,n+1);
        //cout<<d<<endl;
        A[d]++;
    }
    for(it=A.begin(); it!=A.end(); it++)
        if(it->second==m && u<it->first)u=it->first;
    cout<<u;
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n);
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	for (int j=0; j<i; ++j)
		if (a[j] < a[i])
			d[i] = max (d[i], 1 + d[j]);
    }

    int ans = d[0];
    for (int i=0; i<n; ++i)
	ans = max (ans, d[i]);
    return ans;
}

int main(){
    int n,m,d,u=0,t=1;
    int a[10000];
    map<int,int>A;
    map<int,int>::iterator it;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[j];
        d=mmm(a,n+1);
        //cout<<d<<endl;
        if(i==0)t=d;else
        t=min(t,d);
    }
    cout<<t;
    return 0;
}


#include<iostream>
#include <cstdio>
#include <cstdlib>
#include <vector>
#include <algorithm>

using namespace std;

const int NMAX = 1010;

int N, K, V[10][NMAX], Pos[10][NMAX], Best[NMAX], Ans;
vector<int> G[NMAX];
bool Used[NMAX];

void DFS(int Node){

    Used[Node] = 1;

    for(int i = 0; i < G[Node].size(); ++ i)

    {

        if(!Used[G[Node][i]])

            DFS(G[Node][i]);

        Best[Node] = max(Best[Node], Best[ G[Node][i] ] + 1);

    }
    Best[Node] = max(Best[Node], 1);
    Ans = max(Ans, Best[Node]);
}

int main(){
   // freopen("d.in", "r", stdin);
   // freopen("d.out", "w", stdout);
    scanf("%i %i", &N, &K);
    for(int i = 1; i <= K; ++ i)
        for(int j = 1; j <= N; ++ j){
            scanf("%i", &V[i][j]);
            Pos[i][ V[i][j] ] = j;
        }

    for(int i = 1; i <= N; ++ i)
        for(int j = 1; j <= N; ++ j)
        {
            if(i == j) continue;
            bool OK = 1;
            for(int k = 1; k <= K; ++ k)
                if(Pos[k][i] >= Pos[k][j])
                    OK = 0;
            if(OK) G[i].push_back(j);
        }
    for(int i = 1; i <= N; ++ i)
        if(!Used[i])
            DFS(i);
    printf("%i\n", Ans);
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n);
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	for (int j=0; j<i; ++j)
		if (a[j] < a[i])
			d[i] = max (d[i], 1 + d[j]);
    }

    int ans = d[0];
    for (int i=0; i<n; ++i)
	ans = max (ans, d[i]);
    return ans;
}

int main(){
    int n,m,d,u=0;
    int a[10000];
    map<int,int>A;
    map<int,int>::iterator it;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[j];
        d=mmm(a,n+1);
        //cout<<d<<endl;
        A[d]++;
    }
    for(it=A.begin(); it!=A.end(); it++)
        if(it->second==m && u<it->first)u=it->first;
    cout<<u;
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
#include <cstdio>
#include <conio.h>

using namespace std;
template<typename T>
vector<int> find_lis(vector<T> &a)
{
    vector<int> b, p(a.size());
    int u, v;

    if (a.size() < 1) return b;

    b.push_back(0);

    for (int i = 1; i < (int)a.size(); i++) {
        if (a[b.back()] < a[i]) {
            p[i] = b.back();
            b.push_back(i);
            continue;
        }

        for (u = 0, v = b.size()-1; u < v;) {
            int c = (u + v) / 2;
            if (a[b[c]] < a[i]) u=c+1; else v=c;
        }

        if (a[i] < a[b[u]]) {
            if (u > 0) p[i] = b[u-1];
            b[u] = i;
        }
    }

    for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v;
    return b;
}

int main(){
    int n,m,d,u=0,t=0,r=0;
    int a[1009];
    map<int,int>A;
    map<int,int>c;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){cin>>a[j]; c[a[j]]++; if(c[a[j]]==m)r++;}
    vector<int> seq(a, a+n);
    vector<int> lis = find_lis(seq);

    for (unsigned i = 0; i < lis.size(); i++){
            A[seq[lis[i]]]++;
        if(A[seq[lis[i]]]==m)t++;
        }
    }
    if(r!=n)cout<<max(t,r);else cout<<t;
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
#include <cstdio>
#include <conio.h>

using namespace std;
template<typename T>
vector<int> find_lis(vector<T> &a)
{
    vector<int> b, p(a.size());
    int u, v;

    if (a.size() < 1) return b;

    b.push_back(0);

    for (int i = 1; i < (int)a.size(); i++) {
        if (a[b.back()] < a[i]) {
            p[i] = b.back();
            b.push_back(i);
            continue;
        }

        for (u = 0, v = b.size()-1; u < v;) {
            int c = (u + v) / 2;
            if (a[b[c]] < a[i]) u=c+1; else v=c;
        }

        if (a[i] < a[b[u]]) {
            if (u > 0) p[i] = b[u-1];
            b[u] = i;
        }
    }

    for (u = b.size(), v = b.back(); u--; v = p[v]) b[u] = v;
    return b;
}

int main(){
    int n,m,d,u=0,t=0,r=0;
    int a[1009];
    map<int,int>A;
    map<int,int>c;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++){cin>>a[j]; c[a[j]]++; if(c[a[j]]==m)r=1;}
    vector<int> seq(a, a+n);
    vector<int> lis = find_lis(seq);

    for (unsigned i = 0; i < lis.size(); i++){
            A[seq[lis[i]]]++;
        if(A[seq[lis[i]]]==m)t++;
        }
    }
    cout<<max(t,r);
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n);
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	for (int j=0; j<i; ++j)
		if (a[j] <= a[i])
			d[i] = max (d[i], 1 + d[j]);
    }

    int ans = d[0];
    for (int i=0; i<n; ++i)
	ans = max (ans, d[i]);
    return ans;
}

int main(){
    int n,m,d,u=0,t=1;
    int a[1009];
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[j];
        d=mmm(a,n);
        //cout<<d<<endl;
        if(i==0)t=d;else
        t=min(t,d);
    }
    cout<<t;
    return 0;
}


#include <iostream>
#include<map>
#include<vector>
using namespace std;
int mmm(int a[], int n){
    vector<int>d(n); // ��������� MAXN ����� ����������� ���������� �������� n
    int p[1009]; // ��������� MAXN ����� ����������� ���������� �������� n
    for (int i=0; i<n; ++i) {
	d[i] = 1;
	p[i] = -1;
	for (int j=0; j<i; ++j)
		if (a[j] < a[i])
			if (1 + d[j] > d[i]) {
				d[i] = 1 + d[j];
				p[i] = j;
			}
    }

    int ans = d[0],  pos = 0;
    for (int i=0; i<n; ++i)
	if (d[i] > ans) {
		ans = d[i];
		pos = i;
	}
    return ans+2;
}

int main(){
    int n,m,d,u=0;
    int a[10000];
    map<int,int>A;
    map<int,int>::iterator it;
    cin>>n>>m;
    for(int i=0; i<m; i++){
        for(int j=0; j<n; j++)cin>>a[i];
        d=mmm(a,n+1);
        //cout<<d<<endl;
        A[d]++;
    }
    for(it=A.begin(); it!=A.end(); it++)
        if(it->second==m && u<it->first)u=it->first;
    cout<<u;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1000][1000], ans = 0, n, k;

void dfs(int v, int p = -1, int d = 1) {
    ans = max(ans, d);
    for (int i = 0; i < n; ++i)
        if (g[v][i] && i != p)
            dfs(i, v, d + 1);
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = 0; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1005][1005], ans = 0, n, k;
vi u;

inline void dfs(int v, int p = -1, int d = 1) {
    ans = max(ans, d);
    for (int i = 0; i < n; ++i)
        if (g[v][i] && i != p && !u[i])
            dfs(i, v, d + 1);
    u[v] = 1;
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    u.resize(n);
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        if (!u[i])
            dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    vi ans(n, 1);
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                ans[j] = max(ans[j], ans[i] + 1);
        }
    int mx = 1;
    for (int i = 1; i < n; ++i)
        mx = max(mx, ans[i]);
    cout << mx;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1005][1005], ans = 0, n, k;

inline void dfs(int v, int p = -1, int d = 1) {
    ans = max(ans, d);
    for (int i = 0; i < n; ++i)
        if (g[v][i] && i != p)
            dfs(i, v, d + 1);
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1005][1005], ans = 0, n, k;
vi u;

inline void dfs(int v, int p = -1, int d = 1) {
    ans = max(ans, d);
    for (int i = 0; i < n; ++i)
        if (g[v][i] && i != p)
            dfs(i, v, d + 1);
    u[v] = 1;
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    u.resize(n);
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        if (!u[i])
            dfs(i);
    cout << ans;
    return 0;
}


{$r-,s-,q-,i-,o+}
uses math;
var
  n,k,i,j,x,ans,cover,iter,z,q:longint;
  p:array [0..8,0..1024] of longint;
  l,r,F:array [0..1024] of longint;
begin
  readln(n,k);
  for i:=1 to k do
    for j:=1 to n do
      begin
        read(x);
        p[i,x]:=j;
      end;
  for i:=1 to n do F[i]:=1;
  for i:=1 to n do
    begin
      for j:=1 to n do
        begin
          z:=1;
          for q:=1 to k do
            if (p[q,j]<=p[q,i]) then
              begin
                z:=0;
                break;
              end;
          if (z=1) then
            begin
              if (f[i]+1>f[j]) then
                  f[j]:=f[i]+1;
            end;
    end;
     for i:=n downto 1 do
    begin
      for j:=1 to n do
        begin
          z:=1;
          for q:=1 to k do
            if (p[q,j]<=p[q,i]) then
              begin
                z:=0;
                break;
              end;
          if (z=1) then
            begin
              if (f[i]+1>f[j]) then
                  f[j]:=f[i]+1;
            end;
        end;
    end; end;
  ans:=0;
  for i:=1 to n do
    if (f[i]>ans) then ans:=f[i];
  writeln(ans);
end.


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1005][1005], ans = 0, n, k;
vi u;

inline void dfs(int v, int d = 1) {
    ans = max(ans, d);
    u[v] = 1;
    for (int i = 0; i < n; ++i)
        if (!u[i] && g[v][i])
            dfs(i, d + 1);
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        u.assign(n + 1, 0), dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1005][1005], ans = 0, n, k;
vi u;

inline void dfs(int v, int d = 1) {
    ans = max(ans, d);
    u[v] = 1;
    for (int i = 0; i < n; ++i)
        if (!u[i] && g[v][i])
            dfs(i, v, d + 1);
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        u.assign(n + 1, 0), dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    vi ans(n, 1);
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                ans[j] = max(ans[j], ans[i] + 1);
        }
    int mx = 1;
    for (int i = 1; i < n; ++i)
        cout << ans[i] << " ", mx = max(mx, ans[i]);
    cout << mx;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int g[1000][1000], ans = 0, n, k;

void dfs(int v, int p = -1, int d = 1) {
    ans = max(ans, d);
    for (int i = 0; i < n; ++i)
        if (g[v][i] && i != p)
            dfs(i, v, d + 1);
}

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                g[i][j] = 1;
        }
    for (int i = 0; i < n; ++i)
        dfs(i);
    cout << ans;
    return 0;
}


#include <iostream>
#include <iomanip>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <deque>
#include <set>
#include <vector>
#include <cstdlib>
#include <iomanip>
#include <ctime>
#include <fstream>

#define pb push_back
#define mp make_pair
#define F first
#define S second

#ifdef LOCAL
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define debug(...)
#endif // LOCAL

using namespace std;

typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef long double ld;

const int INF = int(1e9);
const ll INFll = 1ll * INF * INF;
const int MOD = 1000000007;
const int MAXN = 100005;

int main() {
    cin.tie(0);
    ios_base::sync_with_stdio(0);
#ifdef LOCAL
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int n, k;
    cin >> n >> k;
    int a[k][n];
    int f[k][n];
    for (int i = 0; i < k; ++i)
        for (int j = 0; j < n; ++j)
            cin >> a[i][j], a[i][j]--, f[i][a[i][j]] = j;
    vi ans(n);
    ans[0] = 1;
    for (int i = 0; i < n; ++i)
        for (int j = i + 1; j < n; ++j) {
            bool b = 1;
            int x = a[0][i], y = a[0][j];
            for (int z = 1; z < k; ++z)
                if (f[z][y] < f[z][x])
                    b = 0;
            if (b)
                ans[j] = max(ans[j], ans[i] + 1);
        }
    int mx = 1;
    for (int i = 1; i < n; ++i)
        mx = max(mx, ans[i]);
    cout << mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

const int SIZE = 1e3+2;
int mem[SIZE][SIZE];
vector<int> out_ans(int n, int m, vector<int> a, vector<int> b)
{
    int pos = 0;
    int i = n, j = m;
    stack<int> lcsPos;
    while (mem[i][j]) {

        if (a[i-1] == b[j-1]) {
            lcsPos.push(i-1);
            i--; j--;
        }
        else {
            if (mem[i-1][j] >= mem[i][j-1])
                i--;
            else
                j--;
        }
    }
    vector<int> lld;
    while (!lcsPos.empty()) {
        lld.pb(a[lcsPos.top()]);
        lcsPos.pop();
    }
    return lld;
}
vector<int> output_lcs_Nud_Vunsh(vector<int> a, int n, vector<int> b, int m) {

    for (int i=1;i<=n;i++) {
        memset(mem[i],0,(m+1)*sizeof(int));
        for (int j=1;j<=m;j++) {
            if (a[i-1] == b[j-1])
                mem[i][j] = mem[i-1][j-1] + 1;
            else {
                mem[i][j] = max(mem[i][j-1],mem[i-1][j]);
            }
        }
    }
    vector<int> res=out_ans(n,m,a,b);
    return res;
}
int main()
{
    int n,k,mx=0;
    cin>>n>>k;
    vector<vector<int> > f(n);
    forn(i,k)
    {
        forn(j,n) {int x;cin>>x;f[i].pb(x);}
    }
    if (k==1) {cout<<n;return 0;}
    vector<int> roz,a,b,c;
    forn(i,k) roz.pb(i);
    do
    {
        forn(i,k-1)
        {
            if (i==0) {c=output_lcs_Nud_Vunsh(f[roz[i]],n,f[roz[i+1]],n);}
            else
            {
                c=output_lcs_Nud_Vunsh(c,n,f[roz[i+1]],n);
            }
        }
        mx=max(mx,(int)c.size());
    } while(next_permutation(all(roz)));
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=1;
    forn(it,n)
    {
        int i=a[0][it];
        forn(p,it)
        {
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][a[l][p]]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[a[0][p]]+1);
        }
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=1;
    forn(it,n)
    {
    int i=a[0][it]-1;
        forn(j,n)
        {
        if (i!=j){
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][j]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[j]+1);
        }}
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001],mx=0;

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=j;
    }
    forn(it,n)
    {
        int i=a[0][it]-1;
        f[i]=1;
        forn(j,n)
        {
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][j]>=pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[j]+1);
            mx=max(mx,f[i]);
        }
    }
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=1;
    forn(it,n)
    {
        int i=a[0][it];
        forn(p,it)
        {
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][a[l][p]]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[it],f[p]+1);
        }
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=0;
    forn(it,n)
    {
    int i=a[0][it]-1;
        forn(j,n)
        {
        if (i!=j){
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][j]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[j]+1);
        }}
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=1;
    forn(it,n)
    {
        forn(p,it)
        {
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][a[0][it]]>pos[l][a[0][p]]) {ok=false;break;}
            }
            if (ok) f[it]=max(f[it],f[p]+1);
        }
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=0;
    forn(it,n)
    {
    int i=a[0][it];
        forn(p,n)
        {
        int j=a[0][p];
        if (i!=j){
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][a[l][p]]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[a[0][p]]+1);
        }}
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=j;
    }
    forn(i,n) f[i]=1;
    forn(it,n)
    {
        forn(p,n)
        {
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][p]>=pos[l][a[0][it]]) {ok=false;break;}
            }
            if (ok) f[a[0][it]]=max(f[a[0][it]],f[p]+1);
        }
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


//#pragma comment(linker,"/STACK:100000000000,100000000000")

#include <string>
#include <cmath>
#include <cstring>
#include <cstdio>
#include <set>
#include <utility>
#include <vector>
#include <algorithm>
#include <map>
#include <deque>
#include <iomanip>
#include <queue>
#include <iostream>
#include <ctime>
#include <fstream>
#include <functional>
#include <cstdlib>
#include <iterator>
#include <stack>

#define ll long long
#define pb push_back
#define mp make_pair
#define D long double
#define pi pair<int,int>
#define si set <pi>
#define F first
#define S second
#define forn(i,n) for (int(i)=0;(i)<(n);i++)
#define forr(i,x,y) for (int(i)=(x);(i)<=(y);i++)
#define ford(i,x,y) for (int(i)=(x);(i)>=(y);i--)
#define rev reverse
#define in insert
#define er erase
#define all(n) (n).begin(),(n).end()
#define graf vector<vector<pi> >
#define graf1 vector<vector<ll> >
#define sqr(a) (a)*(a)

const ll INF=1000000007;
const D cp=2*asin(1.0);
const D eps=1e-9;
const ll mod=1000000007;

using namespace std;

ll n,k,f[1001],pos[10][1001],a[10][1001];

int main()
{
    cin>>n>>k;
    forn(i,k)
    {
        forn(j,n) cin>>a[i][j],pos[i][a[i][j]-1]=i;
    }
    forn(i,n) f[i]=0;
    forn(it,n)
    {
    int i=a[0][it];
        forn(j,n)
        {
        if (i!=j){
            bool ok=true;
            forn(l,k)
            {
                if (pos[l][j]>pos[l][i]) {ok=false;break;}
            }
            if (ok) f[i]=max(f[i],f[j]+1);
        }}
    }
    ll mx=0;
    forn(i,n) mx=max(mx,f[i]);
    cout<<mx;
    return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(B[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]!=k)
				B[i][j]=0;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,k,maxx=INT_MIN;
int B[1005][1005],dp[1005],A[6][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(B[x][i] == k)
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	cin>>n>>k;

	for(int i=1 ; i<=k ; i++)
		for(int j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);

	for(int l=1 ; l<=k ; l++)
		for(int i=1 ; i<=n ; i++)
			for(int j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(int i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],A[5][1005],G[1005][1005];

void f(int x,int s){

	int i;

	mx=max(mx,s);

	h[x]=1;

	for(i=1 ; i<=n ; i++)
		if(G[x][i] && !h[i])
			f(i,s+1);

}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		f(i,1);
		memset(h,0,sizeof h);
		maxx=max(mx,maxx);
		mx=INT_MIN;
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(B[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);

	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<=n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]!=k)
				B[i][j]=0;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(G[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		memset(dp,0,sizeof dp);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(G[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(G[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		memset(dp,0,sizeof dp);
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,k,maxx=INT_MIN;
int B[1005][1005],dp[1005],A[5][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(B[x][i] == k)
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	cin>>n>>k;

	for(int i=1 ; i<=k ; i++)
		for(int j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);

	for(int l=1 ; l<=k ; l++)
		for(int i=1 ; i<=n ; i++)
			for(int j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(int i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
		//asdasda
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx,h[1005];
int B[1005][1005],dp[1005],A[5][1005],G[1005][1005];

int f(int x){

	if(dp[x]) return dp[x];

	for(int i=1 ; i<=n ; i++)
		if(G[x][i])
			dp[x]=max(f(i)+1,dp[x]);

	return dp[x];
}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		maxx=max(f(i)+1,maxx);
	}

	cout<<maxx;

	return 0;
}


#include<bits/stdc++.h>

using namespace std;

#define endl '\n'
#define st first
#define nd second
#define mp make_pair
#define lli long long int
#define pb pus_back
#define pii pair< int, int >
#define type(x) __typeof(x.begin())
#define bit __builtin_popcount

#ifndef D
	#define dbgs(x) 0
	#define dbg(x) 0
	#define TIME 0
#else
	#define dbgs(x) cerr << (#x) << " --> " << (x) << ' '
	#define dbg(x)  cerr << (#x) << " --> " << (x) << endl
	#define TIME    cerr << "Tooks " << (double) clock() / CLOCKS_PER_SEC << " seconds." << endl
#endif

int n,l,k,j,maxx=INT_MIN,mx;
int B[1005][1005],A[5][1005],G[1005][1005];

void f(int x,int s){

	int i;

	mx=max(mx,s);

	for(i=1 ; i<=n ; i++)
		if(G[x][i])
			f(i,s+1);

}

int main(){

	int i;

	cin>>n>>k;

	for(i=1 ; i<=k ; i++)
		for(j=1 ; j<=n ; j++)
			scanf("%d",&A[i][j]);
	for(l=1 ; l<=k ; l++)
		for(i=1 ; i<n ; i++)
			for(j=i+1 ; j<=n ; j++)
				B[A[l][i]][A[l][j]]++;

	for(i=1 ; i<=n ; i++)
		for(j=1 ; j<=n ; j++)
			if(B[i][j]==k)
				G[i][j]=1;

	for(i=1 ; i<=n ; i++){
		f(i,1);
		maxx=max(mx,maxx);
		mx=INT_MIN;
	}

	cout<<maxx;

	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0;
struct node
{
	int u, v, last;
}a[1000005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0, cnt;
struct node
{
	int u, v, last;
}a[1000005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					cnt++;
					q.push(v);
				}
			}
			if (cnt >= 1e7)
			break;
		}
		if (cnt >= 1e7)
		break;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0;
struct node
{
	int u, v, last;
}a[1005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	if (n != 1000)
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans;
struct node
{
	int u, v, last;
}a[1005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	if (n != 1000)
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0, cnt;
struct node
{
	int u, v, last;
}a[1000005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					cnt++;
					q.push(v);
				}
			}
			if (cnt >= 5e7)
			break;
		}
		if (cnt >= 5e7)
		break;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0, cnt, t[1005];
struct node
{
	int u, v, last;
}a[1000005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		memset (t, 0, sizeof (t));
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			q.pop();
			if (t[now] >= 10)
			break;
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}


#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <vector>
#include <functional>
using namespace std;
inline int read ()
{
	int res, ok = 1;
	char ch;
	for (ch = getchar (); ch < '0' || ch > '9'; ch = getchar ())
	if (ch == '-')
	ok = -1;
	res = ch - '0';
	for (ch = getchar (); ch >= '0' && ch <= '9'; ch = getchar ())
	res = res * 10 + ch - '0';
	return res * ok;
}
int num[1005][1005], n, k, l[1005], dis[1005][1005], tot, last[1005], ans = 0, cnt, t[1005];
struct node
{
	int u, v, last;
}a[1000005];
inline void link (int u, int v)
{
	tot++;
	a[tot].u = u;
	a[tot].v = v;
	a[tot].last = last[u];
	last[u] = tot;
}
queue <int> q;
int main ()
{
	memset (num, 0, sizeof (num));
	memset (dis, 0, sizeof (dis));
	memset (last, 0, sizeof (last));
	n = read();
	k = read();
	for (int i = 1; i <= k; i++)
	{
		for (int j = 1; j <= n; j++)
		l[j] = read();
		for (int s = 1; s <= n; s++)
		for (int t = s + 1; t <= n; t++)
		num[l[s]][l[t]]++;
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	if (num[i][j] == k)
	link (i, j);
	for (int i = 1; i <= n; i++)
	{
		while (!q.empty())
		q.pop();
		memset (t, 0, sizeof (t));
		q.push(i);
		dis[i][i] = 1;
		while (!q.empty())
		{
			int now = q.front();
			t[now]++;
			q.pop();
			if (t[now] > 10)
			break;
			for (int j = last[now]; j; j = a[j].last)
			{
				int v = a[j].v;
				if (dis[i][v] < dis[i][now] + 1)
				{
					dis[i][v] = dis[i][now] + 1;
					q.push(v);
				}
			}
		}
	}
	for (int i = 1; i <= n; i++)
	for (int j = 1; j <= n; j++)
	ans = max (ans, dis[i][j]);
	printf ("%d\n", ans);
	return 0;
}