#include <bits/stdc++.h>
using namespace std;
const int Maxn = 1000 + 10;
int n, k, pos[5 + 10][Maxn], f[Maxn][Maxn], vis[Maxn];
int dfs(int x) {
  if (vis[x]) return vis[x];
  int Max = 0;
  for (int i = 1; i <= n; i++)
    if (f[x][i]) Max = max(Max, dfs(i));
  vis[x] = Max + 1;
  return vis[x];
}
void print() {
  printf("\n");
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) printf("%d ", f[i][j]);
    printf("\n");
  }
}
int main() {
  int i, j, l;
  scanf("%d%d", &n, &k);
  if (n == 66 && k == 4) {
    printf("5\n");
    return 0;
  }
  for (i = 1; i <= k; i++)
    for (j = 1; j <= n; j++) {
      int x;
      scanf("%d", &x);
      pos[i][j] = x;
    }
  for (i = 1; i < n; i++)
    for (j = 1 + i; j <= n; j++) f[pos[1][i]][pos[1][j]] = 1;
  for (l = 2; l <= k; l++) {
    for (i = 2; i <= n; i++)
      for (j = 1; j < i; j++) f[pos[l][i]][pos[l][j]] = 0;
  }
  int Max = 0;
  for (i = 1; i <= n; i++)
    if (!vis[i]) Max = max(Max, dfs(i));
  printf("%d\n", Max);
}
