#include <bits/stdc++.h>
using namespace std;
const long long MOD = 1000000007;
vector<long long> g[100010];
long long deg[100010], dp[100010];
void solve() {
  long long n, k;
  cin >> n >> k;
  vector<vector<long long>> v(k, vector<long long>(n));
  for (auto &x : v)
    for (auto &j : x) cin >> j;
  for (long long i = 1; i <= n; i++) {
    long long ct = 0;
    set<long long> a, b;
    bool time = false;
    for (auto x : v) {
      bool fd = false;
      for (long long j = 0; j < x.size(); j++) {
        if (!time and fd)
          b.insert(x[j]);
        else if (time and a.count(x[j]) and fd)
          b.insert(x[j]);
        if (x[j] == i) fd = true;
      }
      time = true;
      a = b;
      b.clear();
    }
    for (auto x : a) g[i].push_back(x), deg[x]++;
  }
  queue<long long> q;
  for (long long i = 1; i <= n; i++) {
    if (!deg[i]) q.push(i), dp[i] = 1;
  }
  while (!q.empty()) {
    long long top = q.front();
    q.pop();
    for (auto x : g[top]) {
      deg[x]--;
      dp[x] = max(dp[x], 1 + dp[top]);
      if (!deg[x]) q.push(x);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1) << '\n';
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  long long t = 1;
  while (t--) solve();
  return 0;
}
