#include <bits/stdc++.h>
using namespace std;
int a[10][1010];
int dir[1010][1010];
vector<int> v[1010];
int k, n, degree[1010];
int dp[1010], ans = -2000000000;
queue<int> q;
void BFS(int from) {
  while (!q.empty()) q.pop();
  if (dp[from] > ans) ans = dp[from];
  q.push(from);
  while (!q.empty()) {
    int t = q.front();
    q.pop();
    int Size = v[t].size();
    for (int i = 0; i < Size; i++) {
      int s = v[t][i];
      if (dp[t] + 1 > dp[s]) {
        dp[s] = dp[t] + 1;
        if (dp[s] > ans) ans = dp[s];
        q.push(s);
      }
    }
  }
}
int main() {
  int i, j, l, x;
  scanf("%d%d", &n, &k);
  for (i = 1; i <= k; i++) {
    for (j = 1; j <= n; j++) {
      scanf("%d", &x);
      a[i][x] = j;
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == j) continue;
      if (dir[j][i]) continue;
      int f = 1;
      for (l = 1; l <= k; l++) {
        if (a[l][i] > a[l][j]) {
          f = 0;
          break;
        }
      }
      if (f) {
        dir[i][j] = 1;
        v[i].push_back(j);
        degree[j]++;
      }
    }
  }
  for (i = 1; i <= n; i++) {
    if (!degree[i]) {
      memset(dp, 0, sizeof(dp));
      dp[i] = 1;
      BFS(i);
    }
  }
  printf("%d\n", ans);
  return 0;
}
