// Original C++ solution (slower):
#include <bits/stdc++.h>

using namespace std;



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

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

#define ITER(it, a) for(typeof(a.begin()) it = a.begin(); it != a.end(); it++)

#define FILL(a, value) memset(a, value, sizeof(a))



#define SZ(a) (int) a.size()

#define ALL(a) a.begin(),a.end()

#define PB push_back

#define MP make_pair



typedef long long LL;

typedef vector<int> VI;

typedef pair<int, int> PII;



const double PI = acos(-1.0);

const LL INF = 1000 * 1000 * 1000 + 7;

const LL LINF = INF * (LL)INF;



const int MAX = 333;



int A[MAX][MAX];

int C[MAX];

int P[MAX];

bool U[MAX];



int main()

{

	//freopen("in.txt", "r", stdin);

	//ios::sync_with_stdio(false); cin.tie(0);



	int n, m;

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

	FOR (i, 0, n)

	{

		FOR (j, 0, m)

		{

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

			A[i][j]--;

		}

	}



	int L = 0, R = n + 1;

	while(R - L > 1)

	{

		int M = (L + R) / 2;



		//M = 1;

		FILL(C, 0);

		FILL(U, 0);

		FOR (i, 0, n)

		{

			P[i] = 0;

			C[A[i][0]]++;

		}



		bool ok = false;



		while(true)

		{

			int ind = -1;

			FOR (i, 0, m)

			{

				if (C[i] > M)

				{

					ind = i;

					break;

				}

			}





			if (ind == -1)

			{

				ok = true;

				break;

			}



			U[ind] = true;



			bool okk = true;



			FOR (i, 0, n)

			{

				while(P[i] < m && U[A[i][P[i]]])

				{

					C[A[i][P[i]]]--;

					P[i]++;

					if (P[i] >= m)

					{

						okk = false;

						break;

					}

					C[A[i][P[i]]]++;

				}

				if (!okk || P[i] >= m)

				{

					okk = false;

					break;

				}

			}



			if (!okk)

			{

				ok = false;

				break;

			}

		}



		if (ok) R = M;

		else L = M;

	}



	cout<<R<<endl;

}


// 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: