#include <bits/stdc++.h>

using namespace std;

#define fi first
#define se second
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dump(x) cerr << #x " = " << (x) << '\n'
#define sz(x) ((int)(x).size())
#define endl '\n'

#ifdef _DEBUG27_
    //#define STRESS 1
#endif

typedef long long llong;

const int INF = 2e9 + 999;
const llong LINF = 2e18 + 999;

void inv(bool e);
template<typename T> T sign(T a);
template<typename T> void die(T &a);
template<typename T> void umax(T &a, T b);
template<typename T> void umin(T &a, T b);

bool brute, alter;
int cnt_tests = 1;

// Improving atcoter rating

// Code below

int n, m;

const int N = 303;

int matrix[N][N];

void inp() {
    cin >> n >> m;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < m; j++) {
            cin >> matrix[i][j];
        }
    }
}

int cur[N], cnt[N];

bool baned[N];

vector<int> pos[N];

bool decrement(int color, int k) {
    //cout << "delete " << color << endl;
    baned[color] = true;
    for (auto x: pos[color]) {
        while (baned[matrix[x][cur[x]]]) {
            cur[x]++;
            if (cur[x] >= m) {
                return false;
            }
        }
        cnt[matrix[x][cur[x]]]++;
        pos[matrix[x][cur[x]]].pb(x);
        cnt[color]--;
        // << color << "-- " << cnt[color] << endl;
        //cout << matrix[x][cur[x]] << "++ " << cnt[matrix[x][cur[x]]] << endl;
        if (cnt[matrix[x][cur[x]]] > k) {
            bool res = decrement(matrix[x][cur[x]], k);
            //cout << "deleted\n";
            if (!res) {
                return false;
            }
        }
    }
    pos[color].clear();
    return true;
}

bool check(int k) {
    //cout << endl << endl;
    //coutcout << "!!! " << k << endl;
    fill(cur, cur + N, 0);
    fill(cnt, cnt + N, 0);
    fill(baned, baned + N, false);
    for (int i = 1; i <= m; i++) {
        pos[i].clear();
    }
    for (int i = 0; i < n; i++) {
        while (baned[matrix[i][cur[i]]]) {
            cur[i]++;
            if (cur[i] >= m) {
                return false;
            }
        }
        cnt[matrix[i][cur[i]]]++;
        pos[matrix[i][cur[i]]].pb(i);
        //cout << matrix[i][cur[i]] << "++ " << cnt[matrix[i][cur[i]]] << endl;
        if (cnt[matrix[i][cur[i]]] > k) {
            bool res = decrement(matrix[i][cur[i]], k);
            //cout << "deleted\n";
            if (!res) {
                return false;
            }
        }
    }
    return true;
}

void solve() {
    int l = 1, r = n, mid;
    while (r - l > 3) {
        mid = (l + r) / 2;
        if (check(mid)) {
            r = mid;
        } else {
            l = mid;
        }
    }
    for (int mid = l; mid <= r; mid++) {
        if (check(mid)) {
            cout << mid;
            return;
        }
    }
}

void stress() {

}

void naive() {

}

void run();
int main() {
    #ifdef _DEBUG27_
        if (1)
            freopen("input.txt", "r", stdin);
    #else
        ios_base::sync_with_stdio(false);
        cin.tie(0);
        cout.tie(0);
        if (0) {
            freopen("palindrome.in", "r", stdin);
            freopen("palindrome.out", "w", stdout);
        }
    #endif
    srand(time(0));
    #ifdef STRESS
        brute = true;
    #else
        brute = false;
    #endif

    for (int i = 0; (i < cnt_tests); i++) {
        run();
    }
    cerr << endl << "Time: " << clock() / 1000.0 << " ms";
    return 0;
}

void run() {
    if (!brute) {
        inp();
    }
    else {
        stress();
    }
    solve();
    cout << endl;
}

template<typename T>
void die(T &a) {
    cout << a;
    exit(0);
}

template<typename T>
T sign(T a) {
    if (a == 0)
        return 0;
    if (a < 0)
        return -1;
    return 1;
}

template<typename T>
void umax(T &a, T b) {
    if (b > a)
        a = b;
}

template<typename T>
void umin(T &a, T b) {
    if (b < a)
        a = b;
}

void inv(bool e) {
    if (!e) {
        vector<int> a;
        a[-1] += 1;
    }
}

/** CLEARING BUFFER

    cout << VAL << flush;

    OR

    printf(VAL);
    fflush(stdout);
*/


Measured memory_bytes: 1280