#include <bits/stdc++.h>
using namespace std;
int n, k, a[10][1001], x[1001][1001], d[1001], res;
bool used[1001];
vector<int> e[1001];
void DFS(int u) {
  used[u] = true;
  for (auto v : e[u]) {
    if (!used[v]) DFS(v);
    d[u] = max(d[u], d[v] + 1);
  }
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) cin >> a[i][j];
  }
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
      for (int t = j + 1; t <= n; t++) x[a[i][j]][a[i][t]]++;
    }
  }
  for (int u = 1; u <= n; u++) {
    for (int v = 1; v <= n; v++) {
      if (x[u][v] == k && u != v) e[u].push_back(v);
    }
  }
  for (int i = 1; i <= n; i++) d[i] = 1;
  for (int i = 1; i <= n; i++) {
    if (!used[i]) DFS(i);
    for (int j = 1; j <= n; j++) res = max(res, d[j]);
  }
  cout << res;
}
