#include <bits/stdc++.h>
using namespace std;
const int MAX_N = 1000 + 20, MAX_K = 5 + 2;
int a[MAX_K][MAX_N];
int dp[MAX_N];
map<int, int> D, ke;
int ans;
int n, k;
int main() {
  ios::sync_with_stdio(false);
  cin >> n >> k;
  for (int i(0); i < int(k); ++i)
    for (int j(0); j < int(n); ++j) {
      cin >> a[i][j];
      --a[i][j];
    }
  for (int i(0); i < int(n); ++i) D[a[0][i]] = i;
  for (int i(0); i < int(k); ++i)
    for (int j(0); j < int(n); ++j) a[i][j] = D[a[i][j]];
  for (int i(0); i < int(n); ++i) {
    ke.clear();
    dp[i] = 1;
    for (int j(0); j < int(k); ++j)
      for (int l = 0; l < n && a[j][l] != i; ++l) ke[a[j][l]]++;
    for (int j(0); j < int(i); ++j)
      if (ke[j] == k) dp[i] = max(dp[i], dp[j] + 1);
  }
  for (int i(0); i < int(n); ++i) ans = max(ans, dp[i]);
  cout << ans << endl;
  return 0;
}
