#include<bits/stdc++.h>
using namespace std;
#define rep(i,n) for(ll i=0;i<n;i++)
#define repl(i,l,r) for(ll i=(l);i<(r);i++)
#define per(i,n) for(ll i=n-1;i>=0;i--)
#define perl(i,r,l) for(ll i=r-1;i>=l;i--)
#define fi first
#define se second
#define pb push_back
#define ins insert
#define pqueue(x) priority_queue<x,vector<x>,greater<x>>
#define all(x) (x).begin(),(x).end()
#define CST(x) cout<<fixed<<setprecision(x)
#define vtpl(x,y,z) vector<tuple<x,y,z>>
#define rev(x) reverse(x);
using ll=long long;
using vl=vector<ll>;
using vvl=vector<vector<ll>>;
using pl=pair<ll,ll>;
using vpl=vector<pl>;
using vvpl=vector<vpl>;
const ll MOD=1000000007;
const ll MOD9=998244353;
const int inf=1e9+10;
const ll INF=4e18;
const ll dy[9]={1,0,-1,0,1,1,-1,-1,0};
const ll dx[9]={0,-1,0,1,1,-1,1,-1,0};
template<class T> inline bool chmin(T& a, T b) {
    if (a > b) {
        a = b;
        return true;
    }
    return false;
}
template<class T> inline bool chmax(T& a, T b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

int main(){
    ll n,m;cin >> n >> m;
    vector<deque<ll>> g(n);
    rep(i,n){
        rep(j,m){
            ll a;cin >> a;
            g[i].pb(a);
        }
    }
    ll ans=inf;
    set<ll> st;
    rep(i,m){
        rep(j,n){
            while(st.count(g[j].front())){
                g[j].pop_front();
            }
        }
        map<ll,ll> mp;
        rep(j,n){
            mp[g[j].front()]++;
        }
        ll del=-1,pp=-1;
        for(auto p:mp){
            if(chmax(pp,p.se))del=p.fi;
        }
        chmin(ans,pp);st.ins(del);
    }
    cout << ans <<endl;
} 
#define C
typedef long long readtype;

/* Header {{{ */
#ifdef C
#include <stdio.h>
#include <math.h>
#include <string.h>
#define isdigit(a) ((a) >= '0' && (a) <= '9' ? true : false)
template<typename type> type abs(type a) { return a < 0 ? -a : a; }
template<typename type> type min(type a, type b) { return a < b ? a : b; }
template<typename type> type beMin(type &a, type b) { return a = (a < b ? a : b); }
template<typename type> type max(type a, type b) { return a > b ? a : b; }
template<typename type> type beMax(type &a, type b) { return a = (a > b ? a : b); }
template<typename type> void swap(type &a, type &b) { type c; c = a; a = b; b = c; }
#endif

#ifdef Cpp
#include <bits/stdc++.h>
using namespace std;
#endif

typedef long long var;

readtype read() {
  readtype a = 0, c = getchar(), s = 0;
  while (!isdigit(c)) s |= c == '-', c = getchar();
  while (isdigit(c)) a = a * 10 + c - 48, c = getchar();
  return s ? -a : a;
}
/* }}} */

const int N = 311;

int n, m;
int a[N][N], have[N], pos[N];
bool ban[N];

bool check(int t);

int main() {
  /*
#ifndef ONLINE_JUDGE
  freopen("source.in", "r", stdin);
  freopen("source.out", "w", stdout);
#endif
*/
  n = read(), m = read();
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= m; ++j) a[i][j] = read();
  }
  int l = 1, r = n;
  while (l < r) {
    int mid = (l + r) >> 1;
    if (!check(mid)) l = mid + 1;
    else r = mid;
  }
  printf("%d\n", l);
  return 0;
}

bool check(int t) {
  memset(ban, false, sizeof(ban));
  memset(have, 0, sizeof(have));
  memset(pos, 0, sizeof(pos));
  for (int i = 1; i <= n; ++i) have[a[i][++pos[i]]]++;
  while (true) {
    bool ok = true;
    for (int i = 1; i <= m; ++i) {
      if (have[i] <= t) continue;
      ok = false;
      ban[i] = true;
    }
    if (ok) break;
    for (int i = 1; i <= n; ++i) {
      while (pos[i] <= m && ban[a[i][pos[i]]]) {
        have[a[i][pos[i]]]--;
        have[a[i][++pos[i]]]++;
      }
      if (pos[i] > m) return false;
    }
  }
  return true;
}
