#include <bits/stdc++.h>
using namespace std;
int n, k;
int A[5][1002];
int occ[5][1002];
int dp[1002];
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) scanf("%d", &A[i][j]);
    for (int j = 0; j < n; j++) {
      occ[i][A[i][j]] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    int a = A[0][i];
    dp[a] = 1;
    for (int j = 1; j < n + 1; j++) {
      bool sahihaiboss = true;
      if (j == a) continue;
      for (int f = 0; f < k; f++) {
        if (occ[f][j] >= occ[f][a]) sahihaiboss = false;
      }
      if (sahihaiboss) {
        dp[a] = max(dp[a], dp[j] + 1);
      }
    }
  }
  int ans = 1;
  for (int i = 1; i < n + 1; i++) ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
