#include <bits/stdc++.h>
using namespace std;
const int N = 2007;
int n, k;
int A[N], dp[N];
char before[N][N];
int go(int v) {
  if (dp[v] != -1) return dp[v];
  int cur = 1;
  for (int to = 1; to <= n; ++to) {
    if (before[to][v]) cur = max(cur, 1 + go(to));
  }
  return dp[v] = cur;
}
int main() {
  cin.tie(0);
  cin >> n >> k;
  memset(before, 1, sizeof(before));
  for (int i = 1; i <= n; ++i) before[i][i] = false;
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j) cin >> A[j];
    for (int j = 0; j < n; ++j)
      for (int u = 0; u < j; ++u) before[A[j]][A[u]] = false;
  }
  memset(dp, -1, sizeof(dp));
  int ans = 0;
  for (int i = 1; i <= n; ++i) ans = max(ans, go(i));
  cout << ans;
  return 0;
}
