#include <bits/stdc++.h>
using namespace std;
const int INF = (int)(1e9);
const long long INF64 = (long long)(INF) * (long long)(INF);
const long double eps = 1e-9;
const int N = 1111 + 9;
int n, k;
int a[6][N];
int dp[N][N];
int pos[6][N];
int main() {
  cin >> n >> k;
  for (int i = 0; i < (int)(k); i++)
    for (int j = 0; j < (int)(n); j++) {
      cin >> a[i + 1][j + 1];
      pos[i + 1][a[i + 1][j + 1]] = j + 1;
    }
  memset(dp, -1, sizeof dp);
  dp[0][0] = 0;
  for (int i = 0; i < n; i++) {
    for (int j = 0; j <= n; j++) {
      int cur = dp[i][j];
      if (cur < 0) continue;
      dp[i + 1][j] = max(dp[i + 1][j], cur);
      bool ok = true;
      int nval = a[1][i + 1];
      for (int l = 1; l <= k; l++) {
        int id1 = pos[l][j];
        int id2 = pos[l][nval];
        if (id1 > id2) ok = false;
      }
      if (ok) dp[i + 1][nval] = max(dp[i + 1][nval], cur + 1);
    }
  }
  int ans = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) ans = max(ans, dp[i][j]);
  cout << ans << endl;
  return 0;
}
