#include <bits/stdc++.h>
using namespace std;
int n, k, a[7][1005], P[7][1005], F[1005];
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j) scanf("%d", &a[i][j]);
    for (int j = 1; j <= n; ++j) {
      for (int k = 1; k <= n; ++k)
        if (a[i][k] == j) {
          P[i][j] = k;
          break;
        }
    }
  }
  memset(F, -1, sizeof F);
  F[0] = 0;
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = i - 1; j >= 0; --j) {
      bool check = true;
      for (int l = 1; l <= k; ++l)
        if (P[l][a[1][j]] > P[l][a[1][i]]) {
          check = false;
          break;
        }
      if (!check) continue;
      F[a[1][i]] = max(F[a[1][i]], F[a[1][j]] + 1);
      res = max(res, F[a[1][i]]);
    }
  }
  cout << res;
  return 0;
}
