#include <bits/stdc++.h>
using namespace std;
vector<int> con[10005];
int s = 0, dp[10005], vis[10005], b[10005];
int a[4][10005];
void rec(int k) {
  vis[k] = 1;
  for (int i = 0; i < con[k].size(); i++)
    if (!vis[con[k][i]]) rec(con[k][i]);
  b[++s] = k;
}
int main() {
  int n, k, i, j, ans, x, y;
  scanf("%d%d", &n, &k);
  for (i = 0; i < k; i++) {
    for (j = 0; j < n; j++) {
      scanf("%d", &x);
      a[i][x] = j;
    }
  }
  for (i = 1; i <= n; i++)
    for (j = 1; j <= n; j++)
      if (i != j) {
        for (y = 0; y < k; y++)
          if (a[y][i] > a[y][j]) break;
        if (y == k) con[i].push_back(j);
      }
  for (i = 1; i <= n; i++)
    if (!vis[i]) rec(i);
  ans = 0;
  for (i = 1; i <= n; i++) {
    dp[b[i]] = 1;
    for (j = 0; j < con[b[i]].size(); j++)
      dp[b[i]] = max(dp[con[b[i]][j]] + 1, dp[b[i]]);
    ans = max(ans, dp[b[i]]);
  }
  printf("%d\n", ans);
  return 0;
}
