#include <algorithm>
#include <iostream>
#include <stack>
#include <queue>
#include <cmath>
#include <string>
#include <iterator>
#include <map>
#include <set>
#include <iomanip>
#include <vector>
#include <cstdint>
using namespace std;
using ll = long long;
using ld = long double;
using P = pair<ll, ll>;
using Graph = vector<vector<int>>;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
#define rep(i, N) for(int i = 0; i < N; i++)
#define rep2(i, l, r) for (ll i = (ll)l; i < (ll)(r); i++)
#define INF 1000000000000000000
#define MAX 200001

const ll MOD = 1000000007;

template <typename T > inline string toString(const T &a) {ostringstream oss; oss << a; return oss.str();};

template<int MOD> struct Fp {
    long long val;
    constexpr Fp(long long v = 0) noexcept : val(v % MOD) {
        if (val < 0) v += MOD;
    }
    constexpr int getmod() { return MOD; }
    constexpr Fp operator - () const noexcept {
        return val ? MOD - val : 0;
    }
    constexpr Fp operator + (const Fp& r) const noexcept { return Fp(*this) += r; }
    constexpr Fp operator - (const Fp& r) const noexcept { return Fp(*this) -= r; }
    constexpr Fp operator * (const Fp& r) const noexcept { return Fp(*this) *= r; }
    constexpr Fp operator / (const Fp& r) const noexcept { return Fp(*this) /= r; }
    constexpr Fp& operator += (const Fp& r) noexcept {
        val += r.val;
        if (val >= MOD) val -= MOD;
        return *this;
    }
    constexpr Fp& operator -= (const Fp& r) noexcept {
        val -= r.val;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr Fp& operator *= (const Fp& r) noexcept {
        val = val * r.val % MOD;
        return *this;
    }
    constexpr Fp& operator /= (const Fp& r) noexcept {
        long long a = r.val, b = MOD, u = 1, v = 0;
        while (b) {
            long long t = a / b;
            a -= t * b; swap(a, b);
            u -= t * v; swap(u, v);
        }
        val = val * u % MOD;
        if (val < 0) val += MOD;
        return *this;
    }
    constexpr bool operator == (const Fp& r) const noexcept {
        return this->val == r.val;
    }
    constexpr bool operator != (const Fp& r) const noexcept {
        return this->val != r.val;
    }
    friend constexpr ostream& operator << (ostream &os, const Fp<MOD>& x) noexcept {
        return os << x.val;
    }
    friend constexpr istream& operator >> (istream &is, Fp<MOD>& x) noexcept {
        return is >> x.val;
    }
    friend constexpr Fp<MOD> modpow(const Fp<MOD> &a, long long n) noexcept {
        if (n == 0) return 1;
        auto t = modpow(a, n / 2);
        t = t * t;
        if (n & 1) t = t * a;
        return t;
    }
};

using mint = Fp<MOD>;

int Del(int M, vector<set<int>> &sport){
    int idx;
    int now = 0;
    rep(i,M){
        int s = sport[i].size();
        if(now < s){
            idx = i;
            now = s;
        }
    }
    return idx;
}

int check(int M, vector<set<int>> &sport){
    int cnt = 0;
    rep(i,M){
        int s = sport[i].size();
        cnt = max(cnt,s);
    }
    return cnt;
}

int main(){
    int N, M;
    cin >> N >> M;
    int ans = 1000;
    vector<set<int>> sport(M);
    vector<queue<int>> A(N);
    vector<int> D(M,0);
    rep(i,N){
        rep(j,M){
            int a;cin >> a;
            a--;
            A[i].push(a);
        }
    }
    rep(t,M){
        if(t != 0){
            int idx = Del(M,sport);
            D[idx] = 1;
            for(auto sp:sport[idx]){
                int a;
                while(true){
                    a = A[sp].front();
                    A[sp].pop();
                    if(D[a] == 0) break;
                }
                sport[a].insert(sp);
            }
            sport[idx].clear();
        }
        else{
            rep(i,N){
                int a = A[i].front();
                A[i].pop();
                sport[a].insert(i);
            }  
        } 
        int res = check(M,sport);
        ans = min(res,ans);
    }
    cout << ans << endl;
}

Measured memory_bytes: 4116