#include <bits/stdc++.h>
int n, k, ans;
int s[5][1010], pos[5][1010];
int f[1010];
inline int max(int a, int b) { return a > b ? a : b; }
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", s[i] + j);
      pos[i][s[i][j]] = j;
      f[j] = 1;
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      int v = s[0][i], u = s[0][j];
      int found = 20040826;
      for (int l = 1; l < k && found; l++)
        if (pos[l][v] < pos[l][u]) found = 0;
      if (found) f[v] = max(f[v], f[u] + 1);
    }
  }
  for (int i = 1; i <= n; i++) ans = max(ans, f[i]);
  printf("%d\n", ans);
  return 0;
}
