#include <bits/stdc++.h>
using namespace std;
const long long N = 1221;
vector<long long> g[N];
long long n, k, a[N], dp[N], ans = 1;
bool is[10][N][N];
int main() {
  cin >> n >> k;
  for (long long i = 1; i <= k; i++) {
    for (long long j = 1; j <= n; j++) cin >> a[j];
    for (long long j = 1; j <= n; j++)
      for (long long u = j + 1; u <= n; u++) is[i][a[j]][a[u]] = 1;
  }
  for (long long i = 1; i <= n; i++) {
    for (long long j = 1; j <= n; j++) {
      bool p = 0;
      for (long long u = 1; u <= k; u++)
        if (!is[u][i][j]) {
          p = 1;
          break;
        }
      if (!p) g[j].push_back(i);
    }
  }
  dp[a[1]] = 1;
  for (long long i = 2; i <= n; i++) {
    dp[a[i]] = 1;
    long long v = a[i];
    for (long long j = 0; j < g[v].size(); j++) {
      long long u = g[v][j];
      dp[v] = max(dp[v], dp[u] + 1);
    }
    ans = max(ans, dp[v]);
  }
  cout << ans;
}
