#include <bits/stdc++.h>
const int N = 1000 + 5;
int A[6][N], n, m, tmp[N], dp[N], pos[6][N];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &A[i][j]);
    }
  }
  for (int i = 1; i <= n; ++i) {
    tmp[A[1][i]] = i;
  }
  for (int i = 2; i <= m; ++i) {
    for (int j = 1; j <= n; ++j) {
      A[i][j] = tmp[A[i][j]];
      pos[i][A[i][j]] = j;
    }
  }
  memset(dp, 0, sizeof(dp));
  for (int i = 0; i < n; ++i) {
    for (int j = i + 1; j <= n; ++j) {
      if (dp[j] >= dp[i] + 1) continue;
      bool flag = true;
      for (int k = 2; k <= m; ++k) {
        if (pos[k][i] > pos[k][j]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        dp[j] = dp[i] + 1;
      }
    }
  }
  int answer = 0;
  for (int i = 1; i <= n; ++i) {
    answer = std::max(answer, dp[i]);
  }
  printf("%d\n", answer);
  return 0;
}
