#include <bits/stdc++.h>
using namespace std;
vector<int> inverse(vector<int> P) {
  vector<int> ans(P.size());
  for (int i = 0; i < P.size(); ++i) ans[P[i]] = i;
  return ans;
}
int grid[1000][1000] = {0};
int dp[1000];
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  srand(time(NULL));
  int n, k;
  cin >> n >> k;
  --k;
  vector<vector<int>> V(k, vector<int>(n));
  vector<int> remap(n);
  for (int i = 0; i < n; ++i) cin >> remap[i], --remap[i];
  remap = inverse(remap);
  for (int i = 0; i < k; ++i)
    for (int j = 0; j < n; ++j) cin >> V[i][j], V[i][j] = remap[V[i][j] - 1];
  for (int i = 0; i < k; ++i) {
    for (int j = 0; j < n; ++j)
      for (int l = 0; l < j; ++l) ++grid[V[i][l]][V[i][j]];
  }
  fill(dp, dp + n, 1);
  for (int i = 1; i < n; ++i)
    for (int j = 0; j < i; ++j) {
      if (grid[j][i] == k) dp[i] = max(dp[i], dp[j] + 1);
    }
  cout << *max_element(dp, dp + n) << '\n';
}
