#include <bits/stdc++.h>
using namespace std;
int k[10][1010];
int dp[1010][1010];
bool vis[1010][1010];
int a[1010], n, m;
int f(int ind, int last) {
  if (ind == 0) return 0;
  if (vis[ind][last]) return dp[ind][last];
  vis[ind][last] = true;
  int ans = f(ind - 1, last);
  bool yes = true;
  for (int i = 1; i < m; i++)
    if (k[i][a[ind]] > k[i][last]) {
      yes = false;
      break;
    }
  if (yes) ans = max(ans, 1 + f(ind - 1, a[ind]));
  return dp[ind][last] = ans;
}
int main() {
  cin.sync_with_stdio(0);
  cin.tie(0);
  int aux;
  cin >> n >> m;
  for (int i = 1; i <= n; i++) cin >> a[i];
  for (int i = 1; i < m; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> aux;
      k[i][aux] = j;
    }
    k[i][0] = n + 1;
  }
  cout << f(n, 0) << "\n";
}
