#include <bits/stdc++.h>
using namespace std;
const int INF = (1LL << 31) - 1;
const long long int LINF = (1LL << 62) - 1;
const int NMAX = 1000 + 5;
const int KMAX = 5 + 3;
int N, K, sol;
int A[KMAX][NMAX];
int P[KMAX][NMAX];
vector<int> V[NMAX];
int viz[NMAX];
void dfs(int x) {
  sol = max(sol, viz[x]);
  for (auto y : V[x])
    if (viz[y] < viz[x] + 1) {
      viz[y] = viz[x] + 1;
      dfs(y);
    }
}
int main() {
  int i, j, k, ok;
  scanf("%d%d", &N, &K);
  for (i = 1; i <= K; i++)
    for (j = 1; j <= N; j++) {
      scanf("%d", &A[i][j]);
      P[i][A[i][j]] = j;
    }
  for (i = 1; i <= N; i++)
    for (j = 1; j <= N; j++) {
      ok = 1;
      for (k = 1; k <= K; k++)
        if (P[k][i] >= P[k][j]) {
          ok = 0;
          break;
        }
      if (ok) V[i].push_back(j);
    }
  for (i = 1; i <= N; i++) {
    memset(viz, 0, sizeof(viz));
    viz[i] = 1;
    dfs(i);
  }
  printf("%d\n", sol);
  return 0;
}
