stablegradients/pie-gem5-bysrc huggingface dataset:
// Original C++ solution (slower):
#include <bits/stdc++.h>



using namespace std;



namespace program

{

	const int MAXN = 300, MAXM = 300, INF = 0x3f3f3f3f;

	bool Vis[MAXM + 10];

	int A[MAXN + 10][MAXM + 10];

	int Pos[MAXN + 10];

	vector<int> T[MAXM + 10];



	void work()

	{

		int n, m, ans = INF;

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

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

		{

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

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

			T[A[i][1]].push_back(i);

			Pos[i] = 1;

		}

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

		{

			int mx = 0;

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

				if(!Vis[j] && T[j].size() > T[mx].size())

					mx = j;

			if(T[mx].size() < ans)

				ans = T[mx].size();

			Vis[mx] = 1;

			for(vector<int>::iterator p = T[mx].begin(); p != T[mx].end(); p++)

			{

				while(Vis[A[*p][Pos[*p]]])

					Pos[*p]++;

				T[A[*p][Pos[*p]]].push_back(*p);

			}

		}

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

	}

}



int main()

{

	program::work();

	return 0;

}


// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

using namespace std;

typedef long long ll;



template<typename T>

void input(T &x) {

	x=0; T a=1;

	register char c=getchar();

	for(;c<48||c>57;c=getchar())

		if(c==45) a=-1;

	for(;c>=48&&c<=57;c=getchar())

		x=x*10+c-48;

	x*=a;

	return;

}



#define MAXN 1010

#define MAXM 1010



int n,m;

int a[MAXN][MAXM];

int sum[MAXM];



namespace solve30 { //O(n*m*2^m)

	

	bool chose[15];



	void main() {

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

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

				input(a[i][j]),a[i][j]--;

		int ans=2147483647;

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

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

				if(s&(1<<i)) chose[i]=true;

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

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

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

						sum[a[i][j]]++;

						break;

					}

			int Max=0;

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

				Max=max(Max,sum[i]),

				sum[i]=0,chose[i]=false;

			}

			ans=min(ans,Max);

		}

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

		return;

	}



}



namespace solve70 { // O(n*m*log n)

	

	int cnt[MAXN];

	bool not_chose[MAXM];



	bool Judge(int mid) {

		memset(not_chose,0,sizeof(not_chose));

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

		while(true) {

			memset(sum,0,sizeof(sum));

			bool flag=true;

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

				for(;cnt[i]<=m&&not_chose[a[i][cnt[i]]];cnt[i]++);

				if(cnt[i]>m) return false;

				if(++sum[a[i][cnt[i]]]>mid)

					not_chose[a[i][cnt[i]]]=true,flag=false;

			}

			if(flag) return true;

		}

		return false;

	}



	void main() {

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

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

				input(a[i][j]);

		int l=1,r=n,ans;

		while(l<=r) {

			int mid=l+r>>1;

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

			else l=mid+1;

		}

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

		return;

	}



}



int main() {

//	freopen("sports.in","r",stdin),

//	freopen("sports.out","w",stdout),

	input(n),input(m);

	if(n<=10&&m<=10) solve30::main();

	else solve70::main();

//	fclose(stdin),fclose(stdout);

	return 0;

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

 

using namespace std;

 

int n, m, ans, at[305], x[305][305], fre[305], ta;

bool rm[305], o;

 

int main() {

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

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

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

			scanf("%d", x[i] + j);

	for (int i = n; i; i--) {

		memset(at, 0, sizeof(at));

		memset(rm, 0, sizeof(rm));

		o = 1;

		while (1) {

			memset(fre, 0, sizeof(fre));

			for (int j = 0; j < n && o; j++) {

				while (at[j] < m && rm[x[j][at[j]]])

					at[j]++;

				if (at[j] == m)

					o = 0;

				else

					fre[x[j][at[j]]]++;

				//printf("! %d %d %d\n",j,at[j],x[j][at[j]]);

			}

			if (!o)

				break;

			ta = -1;

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

				if (fre[j] > i) {

					rm[j] = 1;

					ta = j;

					break;

				}

		//	printf("%d %d\n",i,ta);

			

		//	for (int j = 0; j < n; j++)

		//	printf("%d%c",at[j],j == n-1 ? '\n':' ' );

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

		//	printf("%d%c",fre[j],j == m ? '\n':' ' );

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

		//	printf("%d%c",rm[j],j == m ? '\n':' ' );

			if (ta == -1)

				break;

		}

		if (o)

			ans = i;

		else

			break;

	}

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

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

#define ll int

#define N 305

#define rep(i,j,k) for (ll i=j;i<=k;++i)

#define per(i,j,k) for (ll i=j;i>=k;--i)

using namespace std;

inline ll read(){

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

	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;

}

ll n,m,ans,a[N][N],now[N],flag[N],tmp[N];

int main(){

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

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

	rep(i,1,n) now[i]=1;

	rep(T,1,m-1){

		rep(i,1,n) while (flag[a[i][now[i]]]) ++now[i];

		ll num=0; memset(tmp,0,sizeof tmp);

		memset(tmp,0,sizeof tmp);

		rep(i,1,n) ++tmp[a[i][now[i]]];

		rep(i,1,m) if (tmp[i]>tmp[num]) num=i;

		ans=min(ans,tmp[num]); flag[num]=1;

	}

	printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<cstdio>

#include<cstring>

#include<algorithm>

#include<vector>

#include<set>

#include<cmath>

#include<cstdlib>

#include<iostream>

#include<map>



#define Rep(i,x,y) for (int i=x,_Lim=y;i<=_Lim;i++)

#define Dep(i,x,y) for (int i=x,_Lim=y;i>=_Lim;i--)



#define mk(a,b) make_pair(a,b)



using namespace std;



typedef long long ll;

typedef pair<int,int> PII;



int Stv[1<<8];



inline int read(){

	int f=1,res=0,ch=getchar();

	while (ch<'0' || ch>'9'){

		if (ch=='-') f=-1;ch=getchar();

	}

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

	return f*res;

}

const int N=305;



int n,m,Dat[N][N],B[N],Pos[N],Ind[N];

int main(){

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

	Rep(i,1,n) Rep(j,1,m) Dat[i][j]=read();

	Rep(i,1,n) B[Dat[i][1]]++,Pos[i]=1;

	int Ans=n;

	Rep(k,1,m){

		int pos=0,maxval=0;

		Rep(i,1,m) if (B[i]>maxval){

			pos=i;maxval=B[i];

		}

		Ind[pos]=true;

		Rep(i,1,n) if (Dat[i][Pos[i]]==pos){

			Pos[i]++;

			while (Ind[Dat[i][Pos[i]]]) Pos[i]++;

			B[Dat[i][Pos[i]]]++;

		}

		B[pos]=0;

		if (pos==0) break;

		Ans=min(Ans,maxval);

	}

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

}

/*

4 5

5 1 3 4 2

2 5 3 1 4

2 3 1 4 5

2 5 4 3 1

*/

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

#define ll int

#define N 305

#define rep(i,j,k) for (ll i=j;i<=k;++i)

#define per(i,j,k) for (ll i=j;i>=k;--i)

using namespace std;

inline ll read(){

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

	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;

}

ll n,m,ans,a[N][N],now[N],flag[N],tmp[N];

int main(){

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

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

	rep(i,1,n) now[i]=1;

	rep(T,1,m-1){

		rep(i,1,n) while (flag[a[i][now[i]]]) ++now[i];

		ll num=0; memset(tmp,0,sizeof tmp);

		memset(tmp,0,sizeof tmp);

		rep(i,1,n) ++tmp[a[i][now[i]]];

		rep(i,1,m) if (tmp[i]>tmp[num]) num=i;

		ans=min(ans,tmp[num]); flag[num]=1;

	}

	printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <iostream>

#include <algorithm>

using namespace std;

const int N = 305;

int a[N][N], id[N], cnt[N];

bool vis[N];

int main()

{

	ios::sync_with_stdio(false);

	int n, m;

	cin >> n >> m;

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

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

			cin >> a[i][j];

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

	{

		id[i] = 1;

		cnt[a[i][1]]++;

	}

	int ans = *max_element(cnt + 1, cnt + m + 1);

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

	{

		vis[max_element(cnt + 1, cnt + m + 1) - cnt] = true;

		fill(cnt + 1, cnt + m + 1, 0);

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

		{

			while (vis[a[j][id[j]]])

				id[j]++;

			cnt[a[j][id[j]]]++;

		}

		ans = min(ans, *max_element(cnt + 1, cnt + m + 1));

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<algorithm>

#define ll int

#define N 305

#define rep(i,j,k) for (ll i=j;i<=k;++i)

#define per(i,j,k) for (ll i=j;i>=k;--i)

using namespace std;

inline ll read(){

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

	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;

}

ll n,m,ans,a[N][N],now[N],flag[N],tmp[N];

int main(){

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

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

	rep(i,1,n) now[i]=1;

	rep(T,1,m-1){

		rep(i,1,n) while (flag[a[i][now[i]]]) ++now[i];

		ll num=0; memset(tmp,0,sizeof tmp);

		memset(tmp,0,sizeof tmp);

		rep(i,1,n) ++tmp[a[i][now[i]]];

		rep(i,1,m) if (tmp[i]>tmp[num]) num=i;

		ans=min(ans,tmp[num]); flag[num]=1;

	}

	printf("%d",ans);

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <cstdio>

#include <cstdlib>

#include <cstring>

#include <algorithm>

#include <iostream>

#include <set>

using namespace std;



typedef long long ll;

typedef pair<int, int> pii;



const int KN = 305;



int n, m, a[KN][KN], pos[KN][KN], cnt[KN];



set<pii> S[KN];



bool check(int mid) {

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

		S[i].clear();

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

			S[i].insert(make_pair(j, a[i][j]));

	}

	int idx;

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

		bool can = true;

		memset(cnt, 0x00, sizeof cnt);

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

			++cnt[S[i].begin()->second];

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

			if (cnt[i] > mid)

				can = false, idx = i;

		if (can)

			return true;

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

			S[i].erase(make_pair(pos[i][idx], idx));

	}

	return false;

}



int main() {

	ios::sync_with_stdio(false);

	cin >> n >> m;

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

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

			cin >> a[i][j];

			pos[i][a[i][j]] = j;

		}

	int low = 0, high = n, ans = high;

	while (low <= high) {

		int mid = (low + high) >> 1;

		if (check(mid))

			ans = mid, high = mid - 1;

		else low = mid + 1;

	}

	cout << ans << endl;

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#define N 505

using namespace std;

int n,m,l,r,ans,mid;

int a[N][N],b2[N],MAX;

int read()

{int t=0;char c;

  c=getchar();

  while(!(c>='0' && c<='9')) c=getchar();

  while(c>='0' && c<='9')

	{

	  t=t*10+c-48;c=getchar();

    }

   return t;

}

bool b1[N];

int main()

{int i,j,k;

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

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

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

    ans=n;memset(b1,false,sizeof(b1));

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

	{

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

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

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

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

		{

         b2[a[j][k]]++;break;

        }

		k=1;

        for(j=2;j<=m;j++) if(b2[j]>b2[k])k=j;

        b1[k]=true;

		ans=min(ans,b2[k]);

    }

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

int n,m,a[305][305];

int cnt[305],vis[305],ans=1e9;

int findmax(){

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

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

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

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

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

				break;

			}

	int mx=0;

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

		if (cnt[i]>cnt[mx]) mx=i;

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

	return mx;

}

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<=m;i++)

		vis[findmax()]=1;

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

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#define N 505

using namespace std;

int n,m,l,r,ans,mid;

int a[N][N],b2[N],MAX;

int read()

{int t=0;char c;

  c=getchar();

  while(!(c>='0' && c<='9')) c=getchar();

  while(c>='0' && c<='9')

	{

	  t=t*10+c-48;c=getchar();

    }

   return t;

}

bool b1[N];

int main()

{int i,j,k;

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

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

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

    ans=n;memset(b1,false,sizeof(b1));

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

	{

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

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

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

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

		{

         b2[a[j][k]]++;break;

        }

		k=1;

        for(j=2;j<=m;j++) if(b2[j]>b2[k])k=j;

        b1[k]=true;

		ans=min(ans,b2[k]);

    }

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<stdio.h>

#include<algorithm>

#include<string.h>

#include<math.h>

#include<stdlib.h>

#include<bitset>

#include<map>

#include<queue>

#include<vector>

#include<set>

#include<iostream>

#include<string>

#define ll long long

#define N 305

#define For(i,x,y) for(int i=x;i<=y;i++)

#define Rep(i,x,y) for(int i=x;i>=y;i--)

#define Forn(i,x,y) for(int i=x;i<y;i++)

using namespace std;

int read(){int x=0,f=0;char ch=getchar();for(;ch<'0'||ch>'9';ch=getchar()) if(ch=='-') f=1;for(;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-48;return f?-x:x;}

void write(int x){if(x<10) putchar(x+48);else write(x/10),putchar(x%10+48);}

void writeln(int x){if(x<0) putchar('-'),x=-x;write(x);putchar('\n');}

int n,m,a[N][N],b[N],sum,l[N];

bool flag[N];

int main(){

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

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

	For(i,1,n) b[a[i][1]]++;

	For(i,1,n) l[i]=1;

	For(i,1,m) flag[i]=1;

	For(i,1,m) sum=max(sum,b[i]);

	For(i,1,m-1){

		int mxj=0;For(j,1,m) if(b[j]>b[mxj]) mxj=j;

		flag[mxj]=0;

		For(j,1,n) if(a[j][l[j]]==mxj){

			b[a[j][l[j]]]--;

			while(!flag[a[j][l[j]]]) l[j]++;

			b[a[j][l[j]]]++;

		}

		int mx=0;

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

		sum=min(sum,mx);

	}

	writeln(sum);

	return 0;

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#define N 505

using namespace std;

int n,m,l,r,ans,mid;

int a[N][N],b2[N],MAX;

int read()

{int t=0;char c;

  c=getchar();

  while(!(c>='0' && c<='9')) c=getchar();

  while(c>='0' && c<='9')

	{

	  t=t*10+c-48;c=getchar();

    }

   return t;

}

bool b1[N];

int main()

{int i,j,k;

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

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

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

    ans=n;memset(b1,false,sizeof(b1));

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

	{

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

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

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

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

		{

         b2[a[j][k]]++;break;

        }

		k=1;

        for(j=2;j<=m;j++) if(b2[j]>b2[k])k=j;

        b1[k]=true;

		ans=min(ans,b2[k]);

    }

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

#define Ll long long

using namespace std;

const int N=3e2+5;

int a[N][N],d[N];

bool ok[N];

int ans=1e9,n,m,x;

int find(){

	memset(d,0,sizeof d);

	int ma=0;

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

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

			if(!ok[a[i][j]]){

				d[a[i][j]]++;

				break;

			}

	for(int i=1;i<=m;i++)ma=max(ma,d[i]);

	for(int i=1;i<=m;i++)if(d[i]==ma)ok[i]=1;

	return ma;

}

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]);

	while(1){

		int sum=0;

		for(int i=1;i<=m;i++)if(!ok[i])sum++;

		if(sum==0)break;

		ans=min(find(),ans);

	}

	printf("%d",ans);

}

// Optimized solution to the same problem (faster):
#include<cstdio>

#include<cstring>

#include<cmath>

#include<algorithm>

#define N 505

using namespace std;

int n,m,l,r,ans,mid;

int a[N][N],b2[N],MAX;

int read()

{int t=0;char c;

  c=getchar();

  while(!(c>='0' && c<='9')) c=getchar();

  while(c>='0' && c<='9')

	{

	  t=t*10+c-48;c=getchar();

    }

   return t;

}

bool b1[N];

int main()

{int i,j,k;

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

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

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

    ans=n;memset(b1,false,sizeof(b1));

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

	{

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

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

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

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

		{

         b2[a[j][k]]++;break;

        }

		k=1;

        for(j=2;j<=m;j++) if(b2[j]>b2[k])k=j;

        b1[k]=true;

		ans=min(ans,b2[k]);

    }

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

}

// Fastest known solution for this problem:
// Original C++ solution (slower):
#include<bits/stdc++.h>

using namespace std;

inline void read(int &x)

{

	char c=getchar();

	x=0;

	while (c>'9'||c<'0')

		c=getchar();

	while (c>='0'&&c<='9')

	{

		x=x*10+c-'0';

		c=getchar();

	}

}

const int N=305;

int a[N][N],p[N],f[N],n,m,ans=23333333,pos,b[N];

int main()

{

	read(n);

	read(m);

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

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

			read(a[i][j]);

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

		p[i]=1;

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

	{

		memset(f,0,sizeof(f));

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

		{

			while (b[a[j][p[j]]]&&p[j]<=m)

				++p[j];

			++f[a[j][p[j]]];

		}

		pos=1;

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

			if (f[j]>f[pos])

				pos=j;

		b[pos]=1;

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

			cout<<f[i]<<' ';

		cout<<'\n';*/

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

	}

	cout<<ans;

}

// Optimized solution to the same problem (faster):
#include <stdint.h>

#include <stdio.h>



/* At most three digits. */

unsigned readUnsigned() {

    int c;

    do { c = ::getchar_unlocked(); } while (c < '0');

    unsigned result = c - '0';

    if ((c = ::getchar_unlocked()) >= '0') {

        result *= 10;

        result += c - '0';

        if ((c = ::getchar_unlocked()) >= '0') {

            result *= 10;

            result += c - '0';

        }

    }

    return result;

}



unsigned N, M;

unsigned A[304][304];



bool valid(unsigned x) {

    unsigned I[304] = {0};

    bool bad[304] = {0};

    for (unsigned sports = M;;) {

        unsigned counts[304] = {0};

        for (unsigned i = 0; i < N; ++i) {

            if (++counts[A[i][I[i]]] > x) {

                unsigned badsport = A[i][I[i]];

                bad[badsport] = true;

                if (--sports * x < N) return false;

                for (unsigned j = 0; j < N; ++j) {

                    while (bad[A[j][I[j]]]) {

                        ++I[j];

                    }

                }

                goto next;

            }

        }

        return true;

next:;

    }

    return false;

}



int main() {

    N = readUnsigned();

    M = readUnsigned();

    for (unsigned i = 0; i < N; ++i) {

        for (unsigned j = 0; j < M; ++j) {

            A[i][j] = readUnsigned() - 1;

        }

    }



    unsigned x = 0;

    for (unsigned step = M; step; step /= 2) {

        while (!valid(x + step)) x += step;

    }

    printf("%u\n", x + 1);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



int n, m, temp, ans, a[305][305];



bool C(int ppl)

{

    bool choose[305]; //choose i-th sport

    memset(choose, true, sizeof(choose));



    while(true) {

        int cnt[305]; //count of i-th sport

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

        bool used = true;



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

            int now = 1; //place of i-th sport that can be choose

            for(now=1; now<=m; now++) {

                if(choose[a[i][now]]) break;

            }

            if(now > m) return false;

            cnt[a[i][now]]++;

            if(cnt[a[i][now]] > ppl) {

                choose[a[i][now]] = false;

                used = false;

            }

        }

        if(used) return true;

    }

}



int main()

{

    ios_base::sync_with_stdio(0); cin.tie(0);

    cin >> n >> m;

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

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

            cin >> a[i][j];



    int lb=1, ub=n, mid=0;

    while(lb<=ub) {

        mid = (lb+ub)/2;

        if(C(mid)) {

            ans = mid;

            ub = mid-1;

        }

        else lb = mid+1;

    }

    cout << ans << endl;



    return 0;

}


// Optimized solution to the same problem (faster):
#include <stdint.h>

#include <stdio.h>



/* At most three digits. */

unsigned readUnsigned() {

    int c;

    do { c = ::getchar_unlocked(); } while (c < '0');

    unsigned result = c - '0';

    if ((c = ::getchar_unlocked()) >= '0') {

        result *= 10;

        result += c - '0';

        if ((c = ::getchar_unlocked()) >= '0') {

            result *= 10;

            result += c - '0';

        }

    }

    return result;

}



unsigned N, M;

unsigned A[304][304];



bool valid(unsigned x) {

    unsigned I[304] = {0};

    bool bad[304] = {0};

    for (unsigned sports = M;;) {

        unsigned counts[304] = {0};

        for (unsigned i = 0; i < N; ++i) {

            if (++counts[A[i][I[i]]] > x) {

                unsigned badsport = A[i][I[i]];

                bad[badsport] = true;

                if (--sports * x < N) return false;

                for (unsigned j = 0; j < N; ++j) {

                    while (bad[A[j][I[j]]]) {

                        ++I[j];

                    }

                }

                goto next;

            }

        }

        return true;

next:;

    }

    return false;

}



int main() {

    N = readUnsigned();

    M = readUnsigned();

    for (unsigned i = 0; i < N; ++i) {

        for (unsigned j = 0; j < M; ++j) {

            A[i][j] = readUnsigned() - 1;

        }

    }



    unsigned x = 0;

    for (unsigned step = M; step; step /= 2) {

        while (!valid(x + step)) x += step;

    }

    printf("%u\n", x + 1);

}


// Fastest known solution for this problem:
// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



const int N=505;



int n,m;

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

bool vis[N];



void work()

{

    int ans=n;

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

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

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

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

				if(vis[a[i][j]]){num[a[i][j]]++;break;}

		int mx=0,id=0;

        for(int i=1;i<=m;i++) if(num[i]>=mx) mx=num[i],id=i;

        ans=min(ans,mx);

        vis[id]=0;

	}

	cout<<ans<<endl;

}



void init()

{

	cin>>n>>m;

	for(int i=1;i<=n;i++) for(int j=1;j<=m;j++) cin>>a[i][j];

	for(int i=1;i<=m;i++) vis[i]=1;

}



int main()

{

	init();

	work();

	return 0;

}


// Optimized solution to the same problem (faster):
#include <stdint.h>

#include <stdio.h>



/* At most three digits. */

unsigned readUnsigned() {

    int c;

    do { c = ::getchar_unlocked(); } while (c < '0');

    unsigned result = c - '0';

    if ((c = ::getchar_unlocked()) >= '0') {

        result *= 10;

        result += c - '0';

        if ((c = ::getchar_unlocked()) >= '0') {

            result *= 10;

            result += c - '0';

        }

    }

    return result;

}



unsigned N, M;

unsigned A[304][304];



bool valid(unsigned x) {

    unsigned I[304] = {0};

    bool bad[304] = {0};

    for (unsigned sports = M;;) {

        unsigned counts[304] = {0};

        for (unsigned i = 0; i < N; ++i) {

            if (++counts[A[i][I[i]]] > x) {

                unsigned badsport = A[i][I[i]];

                bad[badsport] = true;

                if (--sports * x < N) return false;

                for (unsigned j = 0; j < N; ++j) {

                    while (bad[A[j][I[j]]]) {

                        ++I[j];

                    }

                }

                goto next;

            }

        }

        return true;

next:;

    }

    return false;

}



int main() {

    N = readUnsigned();

    M = readUnsigned();

    for (unsigned i = 0; i < N; ++i) {

        for (unsigned j = 0; j < M; ++j) {

            A[i][j] = readUnsigned() - 1;

        }

    }



    unsigned x = 0;

    for (unsigned step = M; step; step /= 2) {

        while (!valid(x + step)) x += step;

    }

    printf("%u\n", x + 1);

}


// Fastest known solution for this problem: