#include <bits/stdc++.h>
using namespace std;
int d[2000][2000], t[2000], us[2000], n;
int dfs(int v) {
  if (us[v] == 1) return t[v];
  us[v] = 1;
  for (int i = 1; i <= n; ++i) {
    if (d[v][i] == 1) t[v] = max(dfs(i), t[v]);
  }
  t[v]++;
  return t[v];
}
int m, i, j, k, a[2000], ans;
int main() {
  cin >> n >> m;
  for (i = 1; i <= n; ++i) {
    for (j = 1; j <= n; ++j) d[i][j] = 1;
  }
  for (i = 0; i < m; ++i) {
    for (j = 0; j < n; ++j) {
      cin >> a[j];
      for (k = 0; k <= j; ++k) {
        d[a[j]][a[k]] = 0;
      }
    }
  }
  for (i = 1; i <= n; ++i) {
    ans = max(ans, dfs(i));
  }
  cout << ans << endl;
  return 0;
}
