#include <bits/stdc++.h>
using namespace std;
int n, k, p[6][1005], dp[1005], last[1005], ord[1005][1005], ans = 1,
                                                             pos[6][1005];
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i)
    for (int j = 1; j <= n; ++j)
      cin >> p[i][j], last[p[i][j]] = max(last[p[i][j]], j),
                      pos[i][p[i][j]] = j;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j) {
      if (i == j) continue;
      bool ok = 1;
      for (int t = 1; t <= k && ok; ++t) ok &= (pos[t][i] < pos[t][j]);
      ord[i][j] = ok;
    }
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= k; ++j)
      if (last[p[j][i]] == i) {
        dp[p[j][i]] = 1;
        for (int t = 1; t <= n; ++t) {
          if (t == p[j][i] || !ord[t][p[j][i]]) continue;
          dp[p[j][i]] = max(dp[p[j][i]], dp[t] + 1);
          ans = max(ans, dp[p[j][i]]);
        }
      }
  cout << ans;
  return 0;
}
