#include <bits/stdc++.h>
using namespace std;
int max(int x, int y) {
  if (x > y)
    return x;
  else
    return y;
}
int n, k, Map[1010][1010], f[10100], X, Y, Ans, size[1010][1010];
bool tt;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= n; j++) scanf("%d", &Map[i][j]);
  for (int i = 1; i <= k; i++)
    for (int j = 1; j <= n; j++) size[i][Map[i][j]] = j;
  for (int i = 1; i <= n; i++) {
    X = Map[1][i];
    f[X] = 1;
    for (int j = 1; j <= i - 1; j++) {
      Y = Map[1][j];
      tt = true;
      for (int l = 1; l <= k; l++) {
        if (size[l][Y] > size[l][X]) tt = false;
      }
      if (tt) f[X] = max(f[X], f[Y] + 1);
    }
  }
  for (int i = 1; i <= n; i++) Ans = max(Ans, f[i]);
  printf("%d", Ans);
  return 0;
}
