#include <bits/stdc++.h>
using namespace std;
int dp[1005][6];
int cnt[1005];
int n, k, v[6][1005];
;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &v[i][j]);
      dp[v[i][j]][i] = j;
    }
  }
  int tmax = 0, flag, a, b;
  for (int i = 1; i <= n; ++i) {
    a = v[1][i];
    cnt[a] = 1;
    for (int j = 1; j < i; ++j) {
      b = v[1][j];
      flag = 1;
      if (cnt[b] + 1 <= cnt[a]) continue;
      for (int p = 2; p <= k; ++p)
        if (dp[a][p] < dp[b][p]) flag = 0;
      if (flag) cnt[a] = cnt[b] + 1;
    }
    tmax = cnt[a] > tmax ? cnt[a] : tmax;
  }
  printf("%d\n", tmax);
  return 0;
}
