#include <bits/stdc++.h>
using namespace std;
vector<int> g[1009];
vector<int> perm[6];
bool first[6][1009][1009];
bool visit[1009];
int len[1009];
void dfs(int u) {
  if (len[u] != 0) return;
  int sz = 1;
  for (int i = 0; i < g[u].size(); i++) {
    int v = g[u][i];
    if (!visit[v]) dfs(v);
    sz = max(sz, 1 + len[v]);
  }
  len[u] = sz;
}
int main() {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++) {
      int temp;
      scanf("%d", &temp);
      for (int l = 0; l < perm[i].size(); l++) {
        first[i][perm[i][l]][temp] = true;
      }
      perm[i].push_back(temp);
    }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      bool check = true;
      for (int l = 0; l < k; l++) {
        if (!first[l][i][j]) {
          check = false;
        }
      }
      if (check) g[i].push_back(j);
    }
  }
  int ans = -1;
  for (int i = 1; i <= n; i++) {
    if (len[i] == 0) dfs(i);
    ans = max(ans, len[i]);
  }
  printf("%d\n", ans);
  return 0;
}
