#include <bits/stdc++.h>
using namespace std;
bool after[1005][1005];
int a[10][1005], dp[1005];
int main() {
  int n, k, i, j, l;
  scanf("%d%d", &n, &k);
  for (i = 1; i <= k; i++)
    for (j = 1; j <= n; j++) scanf("%d", &a[i][j]);
  for (i = 1; i <= k; i++)
    for (j = 1; j <= n; j++)
      for (l = 1; l <= j; l++) after[a[i][j]][a[i][l]] = 1;
  int ans = -1;
  for (i = 1; i <= n; i++) {
    dp[i] = 1;
    int tmp = a[1][i];
    for (j = 1; j <= i - 1; j++)
      if (!after[a[1][j]][tmp]) dp[i] = max(dp[i], dp[j] + 1);
    ans = max(dp[i], ans);
  }
  printf("%d\n", ans);
  return 0;
}
