#include <bits/stdc++.h>
using namespace std;
int n, k;
int v[5][1005], u[5][1005];
int dp[1005][1005];
bool mark[1005][1005];
int f(int last, int idx) {
  if (idx == n) return 0;
  if (mark[idx][last]) return dp[idx][last];
  mark[idx][last] = 1;
  int ret = f(last, idx + 1);
  int toLook = v[0][idx + 1];
  bool deu = true;
  for (int i = 1; i < k and deu; i++) {
    int i1 = u[i][last], i2 = u[i][toLook];
    if (i1 > i2) deu = false;
  }
  if (deu) ret = max(ret, f(toLook, idx + 1) + 1);
  return dp[idx][last] = ret;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%d", &v[i][j]);
      u[i][v[i][j]] = j;
    }
  printf("%d\n", f(0, 0));
  return 0;
}
