#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9 + 9;
const long long inf = 1e18 + 18;
const int max6 = 1e6 + 6;
const int modx = 1e9 + 7;
const int mody = 997;
const int base = 137;
vector<int> g[max6];
int dd[1005][1005];
int a[6][1005];
int cl[1005], kc[1005];
int n, k;
set<pair<int, int>, greater<pair<int, int>>> s;
int best = 0;
void dij(int xp) {
  for (int i = 1; i <= n; ++i) cl[i] = kc[i] = 0;
  s.insert({0, xp});
  cl[xp] = 1;
  while (!s.empty()) {
    pair<int, int> pe = *s.begin();
    s.erase(pe);
    int u = pe.second;
    for (auto& v : g[u])
      if (cl[v] == 0) {
        cl[v] = 1;
        kc[v] = kc[u] + 1;
        s.insert({kc[v], v});
      } else if (kc[v] < kc[u] + 1) {
        s.erase({kc[v], v});
        kc[v] = kc[u] + 1;
        s.insert({kc[v], v});
      }
  }
  for (int i = 1; i <= n; ++i) best = max(best, kc[i]);
}
int deg[max6];
int main() {
  ios_base::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    for (int j = 1; j <= n; ++j) cin >> a[i][j];
  for (int i = 1; i <= k; ++i) {
    for (int u = 1; u <= n; ++u)
      for (int v = u + 1; v <= n; ++v) dd[a[i][u]][a[i][v]]++;
  }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      if (dd[i][j] == k) g[i].push_back(j), deg[j] = 1;
  for (int i = 1; i <= n; ++i)
    if (deg[i] == 0) dij(i);
  cout << best + 1;
}
