stablegradients/pie-gem5-pairs huggingface dataset:
// Slower C++ solution:
#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;

}

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

#include<cstring>

#include<algorithm>

#include<vector>

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

using namespace std;



inline int read() {

	char ch = getchar(); int x = 0, op = 1;

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

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

	return op * x;

}



const int maxn = 309;

int a[maxn][maxn], p[maxn], del[maxn], cnt[maxn];

int n, m, l = 1, r, mid;



bool check(int limit) {

	int mx = 0, cur = 0, now, ty;

	memset(cnt, 0, sizeof cnt);

	memset(del, 0, sizeof del);

	rep (i, 1, n) {

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

	}

	rep (i, 1, m) if (cnt[i] > mx) {

		mx = cnt[i]; cur = i; 

	}

	while (mx > limit) {

		del[cur] = 1;

		rep (i, 1, n) {

			if (!del[a[i][p[i]]]) continue;

			while (p[i] <= m && del[a[i][p[i]]]) p[i]++;

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

			cnt[a[i][p[i]]]++;

		}

		mx = 0;

		rep (i, 1, m) if (!del[i] && cnt[i] > mx) {

			mx = cnt[i]; cur = i;

		}

	}

	return 1;

}



int main() {

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

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

	while (l < r) {

		mid = (l + r) >> 1;

		if (check(mid)) r = mid;

		else l = mid + 1;

	}

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

	return 0;

}
// Slower C++ solution:
#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

*/

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

#include<cstring>

#include<algorithm>

#include<vector>

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

using namespace std;



inline int read() {

	char ch = getchar(); int x = 0, op = 1;

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

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

	return op * x;

}



const int maxn = 309;

int a[maxn][maxn], p[maxn], del[maxn], cnt[maxn];

int n, m, l = 1, r, mid;



bool check(int limit) {

	int mx = 0, cur = 0, now, ty;

	memset(cnt, 0, sizeof cnt);

	memset(del, 0, sizeof del);

	rep (i, 1, n) {

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

	}

	rep (i, 1, m) if (cnt[i] > mx) {

		mx = cnt[i]; cur = i; 

	}

	while (mx > limit) {

		del[cur] = 1;

		rep (i, 1, n) {

			if (!del[a[i][p[i]]]) continue;

			while (p[i] <= m && del[a[i][p[i]]]) p[i]++;

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

			cnt[a[i][p[i]]]++;

		}

		mx = 0;

		rep (i, 1, m) if (!del[i] && cnt[i] > mx) {

			mx = cnt[i]; cur = i;

		}

	}

	return 1;

}



int main() {

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

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

	while (l < r) {

		mid = (l + r) >> 1;

		if (check(mid)) r = mid;

		else l = mid + 1;

	}

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

	return 0;

}
// Slower C++ solution:
#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;

}

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

#include<cstring>

#include<algorithm>

#include<vector>

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

using namespace std;



inline int read() {

	char ch = getchar(); int x = 0, op = 1;

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

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

	return op * x;

}



const int maxn = 309;

int a[maxn][maxn], p[maxn], del[maxn], cnt[maxn];

int n, m, l = 1, r, mid;



bool check(int limit) {

	int mx = 0, cur = 0, now, ty;

	memset(cnt, 0, sizeof cnt);

	memset(del, 0, sizeof del);

	rep (i, 1, n) {

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

	}

	rep (i, 1, m) if (cnt[i] > mx) {

		mx = cnt[i]; cur = i; 

	}

	while (mx > limit) {

		del[cur] = 1;

		rep (i, 1, n) {

			if (!del[a[i][p[i]]]) continue;

			while (p[i] <= m && del[a[i][p[i]]]) p[i]++;

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

			cnt[a[i][p[i]]]++;

		}

		mx = 0;

		rep (i, 1, m) if (!del[i] && cnt[i] > mx) {

			mx = cnt[i]; cur = i;

		}

	}

	return 1;

}



int main() {

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

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

	while (l < r) {

		mid = (l + r) >> 1;

		if (check(mid)) r = mid;

		else l = mid + 1;

	}

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

	return 0;

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.271725578145633x):
#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;

}



int n,m;



namespace solve70 {

	

	const int N=310;

	const int M=310;

	int a[N][M],cnt[N],sum[N];

	bool go[M];



	bool Judge(int mid) {

		memset(go,0,sizeof(go));

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

		while(1) {

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

			bool flag=true;

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

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

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

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

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

			}

			if(flag) return true;

		}

		return false;

	}



	int main() {

		input(n),input(m);

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

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

				input(a[i][j]);

		int l=1,r=n;

		while(l<r) {

			int mid=l+r>>1;

			if(Judge(mid)) r=mid;

			else l=mid+1;

		}

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

		return 0;

	}



}



int main() { return solve70::main(); }
// Slower C++ solution:
#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

*/

// Faster C++ solution for the same problem (measured speedup: 5.198482145108377x):
#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;

}



int n,m;



namespace solve70 {

	

	const int N=310;

	const int M=310;

	int a[N][M],cnt[N],sum[N];

	bool go[M];



	bool Judge(int mid) {

		memset(go,0,sizeof(go));

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

		while(1) {

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

			bool flag=true;

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

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

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

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

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

			}

			if(flag) return true;

		}

		return false;

	}



	int main() {

		input(n),input(m);

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

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

				input(a[i][j]);

		int l=1,r=n;

		while(l<r) {

			int mid=l+r>>1;

			if(Judge(mid)) r=mid;

			else l=mid+1;

		}

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

		return 0;

	}



}



int main() { return solve70::main(); }
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.343536817459916x):
#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;

}



int n,m;



namespace solve70 {

	

	const int N=310;

	const int M=310;

	int a[N][M],cnt[N],sum[N];

	bool go[M];



	bool Judge(int mid) {

		memset(go,0,sizeof(go));

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

		while(1) {

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

			bool flag=true;

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

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

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

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

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

			}

			if(flag) return true;

		}

		return false;

	}



	int main() {

		input(n),input(m);

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

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

				input(a[i][j]);

		int l=1,r=n;

		while(l<r) {

			int mid=l+r>>1;

			if(Judge(mid)) r=mid;

			else l=mid+1;

		}

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

		return 0;

	}



}



int main() { return solve70::main(); }
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.500816504716352x):
#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);

}
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 5.257198387985189x):
#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);

}
// Slower C++ solution:
#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;

}

// Faster C++ solution for the same problem (measured speedup: 5.215720328596217x):
#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);

}
// Slower C++ solution:
#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);

}

// Faster C++ solution for the same problem (measured speedup: 5.255216788335286x):
#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);

}
// Slower C++ solution:
#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;

}

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

#include<cstring>

#include<cmath>

#include<algorithm>

#define For(i,j,k) for(int i=j;i<=k;i++)

#define Dow(i,j,k) for(int i=j;i>=k;i--)

#define LL long long

using namespace std;



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-48 ; ch = getchar(); }

	return x * f ;

}



const int N = 311;

int n,m,ans;

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



int main() {

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

	For(i, 1, n) 

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

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

	ans = n;

	For(T, 1, m) {

		memset(cnt,0,sizeof cnt);

		For(i, 1, n) 

		  For(j, 1, m) 

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

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

		    	break;

			}

		int Mx = 0,id = 0;

		For(i, 1, m) 

			if( cnt[i] > Mx ) Mx = cnt[i],id = i;

		ans = min(Mx,ans);

		vis[id] = 0;

	} 

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

	return 0;

}