#include <bits/stdc++.h>
using namespace std;
int dp[6][1005][1005];
int a[1005];
vector<int> mp[1005];
int vis[1005];
int ans[1005];
int mx = 0;
int ru[1005];
int n, k;
queue<int> qu;
void solve() {
  memset(vis, 0, sizeof(vis));
  for (int i = 1; i <= n; i++) {
    if (ru[i] == 0) {
      qu.push(i);
    }
  }
  while (!qu.empty()) {
    int tmp = qu.front();
    qu.pop();
    for (int i = 0; i < mp[tmp].size(); i++) {
      ru[mp[tmp][i]]--;
      ans[mp[tmp][i]] = max(ans[tmp] + 1, ans[mp[tmp][i]]);
      if (ru[mp[tmp][i]] == 0) {
        qu.push(mp[tmp][i]);
      }
    }
    mx = max(mx, ans[tmp]);
  }
}
int main() {
  scanf("%d %d", &n, &k);
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &a[j]);
      for (int s = 1; s < j; s++) {
        dp[i][a[s]][a[j]] = 1;
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      dp[0][i][j] = dp[1][i][j];
      for (int s = 2; s <= k; s++) {
        dp[0][i][j] = dp[0][i][j] & dp[s][i][j];
      }
      if (dp[0][i][j] == 1) {
        ru[j]++;
        mp[i].push_back(j);
      }
    }
  }
  solve();
  printf("%d\n", mx + 1);
  return 0;
}
