stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#include<bits/stdc++.h>

#define maxn 500

#define rt register int

using namespace std;

int n,m;

int a[maxn][maxn],vis[maxn],cnt[maxn];

inline int min(int a,int b){

	return a<b?a:b;

}

inline int read()

{

    int x=0,f=0;char s=getchar();

    while(!isdigit(s))f|=s=='-',s=getchar();

    while( isdigit(s))x=(x<<1)+(x<<3)+s-48,s=getchar();

    return !f?x:-x;

}

inline void print(int x)

{

    if(x/10>0)print(x/10);

    putchar(x%10+'0');

}

int main(){

    n=read();m=read();

    for (rt i=1;i<=n;i++)

        for (rt j=1;j<=m;j++)

            a[i][j]=read();

    int ans=n;

    for (rt i=1;i<=m;i++){

        memset(cnt,0,sizeof(cnt));

        for (rt j=1;j<=n;j++)

            for (rt k=1;k<=m;k++)

                if (!vis[a[j][k]]){

                    cnt[a[j][k]]++;

                    break;

                }

        int s,sum=0;

        for (rt j=1;j<=m;j++)

            if (sum<cnt[j])

                s=j,sum=cnt[j];

        vis[s]=1;

        ans=min(ans,sum);

    }

    print(ans);

    return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.0954149923802525x):
#include <cstdio>

#include <cstring>

#include <algorithm>

using namespace std;



const int MAXN = 305;

int g[MAXN][MAXN], pro[MAXN];

bool be_deleted[MAXN];



int main() {

	int n, m;

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

	for (register int i = 1; i <= n; i++)

		for (register int j = 1; j <= m; j++)

			scanf("%d", &g[i][j]);

	int ans = n;

	for (register int i = 1; i <= m; i++) {

		memset(pro, 0, sizeof(pro));

		for (register int j = 1; j <= n; j++)

			for (register int k = 1; k <= m; k++) {

				int temp = g[j][k];

				if (!be_deleted[temp]) {

					pro[temp]++;

					break;

				}

			}

		int pos = int(max_element(pro+1, pro+1+m) - pro);

		ans = min(ans, pro[pos]);

		be_deleted[pos] = true;

	}

	printf("%d", ans);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

int n,m,ans;

int a[305][305],sum[305];

bool vis[305];

int main(){

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	ans=n;

	for(int i=1;i<=m;i++)

	{

		for(int j=1;j<=m;j++) sum[j]=0;

		for(int j=1;j<=n;j++)

			for(int k=1;k<=m;k++)

				if( !vis[a[j][k]] ) 

				{

					++sum[a[j][k]]; 

					break;

				}

		int k=1;

		for(int j=1;j<=m;j++) 

			if(sum[j]>sum[k]) k=j;

		vis[k]=true;

		if(sum[k]<ans) ans=sum[k];

	}

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

}

// Faster C++ solution for the same problem (measured speedup: 5.229615732444326x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

#define ll long long

#define For(i,a,b) for (int i=a;i<=b;i++)

#define Rep(i,a,b) for (int i=b;i>=a;i--)

using namespace std;

inline ll read()

{

    ll x=0,f=1;char ch=getchar();

    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

    while(ch>='0'&&ch<='9')x=x*10+ch-'0',ch=getchar();

    return x*f;

} 

int n,m,k,ans;  

int a[305][305],b[305];  

bool ban[305];  

int main()

{  

    n=read();m=read();

    For(i,1,n)For(j,1,m)a[i][j]=read();

    ans=n;  

    For(i,1,m)

    {

    	For(j,1,m)b[j]=0;

    	For(j,1,n)

    		For(k,1,m)

    			if(!ban[a[j][k]])

    			{

    				b[a[j][k]]++;

    				break;

				}

		int k=1;

		For(j,1,m)

			if(b[j]>b[k])k=j;

		ban[k]=true;

		if(b[k]<ans)ans=b[k];

	}

	cout<<ans<<endl;

    return 0;  

}  


// Faster C++ solution for the same problem (measured speedup: 5.1889939749701846x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.7595287728871325x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1133207271905903x):
#include <bits/stdc++.h>

using namespace std;

#define ll long long

#define N 310

int ans=0x3f3f3f3f,n,m,a[N][N],cnt[N],flag[N],now[N];

inline char gc(){

    static char *S,*T,buf[1<<16];

    if(T==S){T=(S=buf)+fread(buf,1,1<<16,stdin);if(T==S) return EOF;}

    return *S++;

}

inline int read(){

    int x=0,f=1;char ch=gc();

    while(ch<'0' || ch>'9'){if(ch=='-') f=-1;ch=gc();}

    while('0'<=ch && ch<='9') x=x*10+ch-'0',ch=gc();

    return x*f;

}

int main(){

	n=read();m=read();

	for(int i=1;i<=n;++i)

		for(int j=1;j<=m;++j)

			a[i][j]=read();

	for(int i=1;i<=n;++i) now[i]=1;

	for(int i=1;i<=m;++i){

		for(int j=1;j<=m;++j) cnt[j]=0;

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

			while(flag[a[j][now[j]]]) ++now[j];

			cnt[a[j][now[j]]]++;

		}

		int mx=0;

		for(int j=1;j<=m;++j) if(cnt[j]>=cnt[mx]) mx=j;

		flag[mx]=1;

		ans=min(ans,cnt[mx]);

	}

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

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 1.1126983842604201x):
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

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

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

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

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

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

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

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

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.176648140578894x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.759492613251796x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

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

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

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

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.176148985854754x):
#include<cstdio>

#include<algorithm>

#include<cstring>

using namespace std;

const int inf=1e8,maxn=305;

inline int read(){int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){if(ch=='-') f=-1; ch=getchar();} while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();} return x*f;}

int n,m,ans,cnt[maxn],a[maxn][maxn];

bool cho[maxn];

inline void init(){

	n=read(); m=read();

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

		for (int j=1;j<=m;j++){

			a[i][j]=read();

		}

	}

	ans=inf;

	memset(cho,1,sizeof(cho));

	for (int k=1;k<=m;k++){

		memset(cnt,0,sizeof(cnt));

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

			for (int j=1;j<=m;j++){

				if (cho[a[i][j]]){

					cnt[a[i][j]]++;

					break;

				}

			}

		}

		int mx=0;

		for (int i=1;i<=m;i++){

			if ((!mx)||(mx&&cnt[mx]<cnt[i])){

				mx=i;

			}

		}

		cho[mx]=0;

		ans=min(cnt[mx],ans);

	}

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

}

int main(){

	init();

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

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

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

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

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.105024628218113x):
#include<stdio.h>

#include<cstring>

#include<algorithm>

using namespace std;

#define N 305



int n,m,i,j,l,r,mid,Max,ans;

int a[N][N],num[N],pos[N];

bool vis[N];



void get(){Max=0;

	for(int i=1;i<=m;i++)

		if(num[i]>num[Max])Max=i;

}

bool check1(int x){

	memset(num,0,sizeof(num));

	memset(vis,0,sizeof(vis));

	for(int i=1;i<=n;i++)num[a[i][1]]++,pos[i]=1;

	while(get(),num[Max]>x){

		num[Max]=0;vis[Max]=1;

		for(int i=1;i<=n;i++)

			if(a[i][pos[i]]==Max){

				while(vis[a[i][pos[i]]]&&pos[i]<=m)pos[i]++;

				if(pos[i]>m)return 0;

				num[a[i][pos[i]]]++;

			}

	}return 1;

}

int main()

{

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

	for(i=1;i<=n;i++)

	for(j=1;j<=m;j++)

		scanf("%d",&a[i][j]);

	l=1,r=n;

	while(l<=r){

		mid=(l+r)>>1;

		if(check1(mid))ans=mid,r=mid-1;

			else l=mid+1;

	}printf("%d\n",ans);

	return 0;

}
// Slower C++ solution:
#include<bits/stdc++.h>

using namespace std;

const int N=305;

int a[N][N];

bool b[N];

queue<int>f[N];

int n,m;

int main()

{

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

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)

			scanf("%d",&a[i][j]);

	for(int i=1;i<=n;i++)

		f[a[i][1]].push(i),a[i][0]=1;

	memset(b,true,sizeof(b));

	int ret=0;

	for(int i=1;i<=m;i++)

		if(f[i].size()>ret)ret=f[i].size();

	int ans=ret;

	while(1){

		int k=-1;

		for(int i=1;i<=m;i++)

			if(k==-1||f[i].size()>f[k].size())k=i;

		if(k==-1)break;

		b[k]=false;

		while(!f[k].empty()){

			int i=f[k].front();

			f[k].pop();

			while(!b[a[i][a[i][0]]])a[i][0]++;

			f[a[i][a[i][0]]].push(i);

		}

		int ret=0;

		for(int i=1;i<=m;i++)

			if(f[i].size()>ret)ret=f[i].size();

		if(ret==0)break;

		ans=min(ans,ret);

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.679804931343608x):
#include<stdio.h>

#include<cstring>

#include<algorithm>

using namespace std;

#define N 305



int n,m,i,j,l,r,mid,Max,ans;

int a[N][N],num[N],pos[N];

bool vis[N];



void get(){Max=0;

	for(int i=1;i<=m;i++)

		if(num[i]>num[Max])Max=i;

}

bool check1(int x){

	memset(num,0,sizeof(num));

	memset(vis,0,sizeof(vis));

	for(int i=1;i<=n;i++)num[a[i][1]]++,pos[i]=1;

	while(get(),num[Max]>x){

		num[Max]=0;vis[Max]=1;

		for(int i=1;i<=n;i++)

			if(a[i][pos[i]]==Max){

				while(vis[a[i][pos[i]]]&&pos[i]<=m)pos[i]++;

				if(pos[i]>m)return 0;

				num[a[i][pos[i]]]++;

			}

	}return 1;

}

int main()

{

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

	for(i=1;i<=n;i++)

	for(j=1;j<=m;j++)

		scanf("%d",&a[i][j]);

	l=1,r=n;

	while(l<=r){

		mid=(l+r)>>1;

		if(check1(mid))ans=mid,r=mid-1;

			else l=mid+1;

	}printf("%d\n",ans);

	return 0;

}
// Slower C++ solution:
#include <cstdio>

#include <algorithm>

#include <cmath>

#include <cstring>

#include <iostream>

#include <ctime>

#include <map>

#include <queue>

#include <cstdlib>

#include <string>

#include <climits>

#include <set>

#include <vector>

using namespace std;

inline int read(){

	int k=0,f=1;char ch=getchar();

	while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}

	while(ch>='0'&&ch<='9'){k=k*10+ch-'0';ch=getchar();}

	return k*f;

}

int a[310][310],n,m,b[310],c[310];

bool flag;

inline void clear(int p,int x){

	if(!flag)return;b[p]=-1;

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

		int cnt=0;

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++,cnt++;

		if(cnt==0)continue;

		if(c[i]>m){flag=0;return;}

		b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

}

inline bool check(int x){

	memset(b,0,sizeof b);

	flag=1;

	for(int i=1;i<=n;i++)c[i]=1;

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

		while(c[i]<=m&&b[a[i][c[i]]]==-1)c[i]++;

		if(c[i]>m)return 0;

		if(c[i]==1)b[a[i][c[i]]]++;

		if(b[a[i][c[i]]]>x)clear(a[i][c[i]],x);

	}

	return flag;

}

int main()

{

	n=read();m=read();

	for(int i=1;i<=n;i++)

		for(int j=1;j<=m;j++)a[i][j]=read();

	int l=0,r=n,ans;

	while(l<=r){

		int mid=l+r>>1;

		if(check(mid))ans=mid,r=mid-1;

		else l=mid+1;

	}

	printf("%d",ans);

	return 0;

}

// Faster C++ solution for the same problem (measured speedup: 5.104532379741719x):
#include<stdio.h>

#include<cstring>

#include<algorithm>

using namespace std;

#define N 305



int n,m,i,j,l,r,mid,Max,ans;

int a[N][N],num[N],pos[N];

bool vis[N];



void get(){Max=0;

	for(int i=1;i<=m;i++)

		if(num[i]>num[Max])Max=i;

}

bool check1(int x){

	memset(num,0,sizeof(num));

	memset(vis,0,sizeof(vis));

	for(int i=1;i<=n;i++)num[a[i][1]]++,pos[i]=1;

	while(get(),num[Max]>x){

		num[Max]=0;vis[Max]=1;

		for(int i=1;i<=n;i++)

			if(a[i][pos[i]]==Max){

				while(vis[a[i][pos[i]]]&&pos[i]<=m)pos[i]++;

				if(pos[i]>m)return 0;

				num[a[i][pos[i]]]++;

			}

	}return 1;

}

int main()

{

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

	for(i=1;i<=n;i++)

	for(j=1;j<=m;j++)

		scanf("%d",&a[i][j]);

	l=1,r=n;

	while(l<=r){

		mid=(l+r)>>1;

		if(check1(mid))ans=mid,r=mid-1;

			else l=mid+1;

	}printf("%d\n",ans);

	return 0;

}