#include <bits/stdc++.h>
using namespace std;
const long long PR = 1000000009;
int n, k, A[6][1009], dp[1009], pos[1009][1009], ans = 1;
vector<int> frdp[1009];
using namespace std;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      cin >> A[i][j];
      pos[i][A[i][j]] = j;
    }
  }
  memset(dp, 1, sizeof(dp));
  for (int i = n - 1; i >= 0; i--) {
    for (int j = 1; j < k; j++) {
      frdp[i].push_back(pos[j][A[0][i]]);
    }
    dp[i] = 1;
    for (int j = i + 1; j < n; j++) {
      bool condn = true;
      for (int k1 = 0; k1 < k - 1; k1++)
        if (frdp[j][k1] <= frdp[i][k1]) condn = false;
      if (condn) {
        dp[i] = max(dp[i], 1 + dp[j]);
      }
    }
    ans = max(dp[i], ans);
  }
  cout << ans << endl;
  return 0;
};
