#include <bits/stdc++.h>
using namespace std;
int n, k;
int position[5][1000], a[5][1000], dp[1000];
int main() {
  while (cin >> n >> k) {
    for (int i = 0; i < k; i++) {
      for (int j = 0; j < n; j++) {
        scanf("%d", &a[i][j]);
        position[i][--a[i][j]] = j;
      }
    }
    int ans = 0;
    for (int j = 0; j < n; ++j) {
      int maxn = 0;
      for (int p = 0; p < j; ++p) {
        int i;
        for (i = 1; i < k && position[i][a[0][p]] < position[i][a[0][j]]; ++i)
          ;
        if (i == k && dp[p] > maxn) maxn = dp[p];
      }
      dp[j] = maxn + 1;
      ans = max(ans, dp[j]);
    }
    printf("%d\n", ans);
  }
  return 0;
}
