#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:102400000,102400000")
using namespace std;
const long long mod = 1000000007;
int n, m;
int go[1009][1009];
int num[6][1009];
int dis[1009];
int vis[1009];
void spfa() {
  memset((dis), (0), sizeof((dis)));
  memset((vis), (0), sizeof((vis)));
  queue<int> q;
  while (!q.empty()) q.pop();
  q.push(0);
  vis[0] = 1;
  while (!q.empty()) {
    int a = q.front();
    q.pop();
    for (int i = 1; i <= n; i++) {
      if (go[a][i] == 0) continue;
      if (dis[a] + go[a][i] > dis[i]) {
        dis[i] = dis[a] + go[a][i];
        if (!vis[i]) {
          q.push(i);
          vis[i] = 1;
        }
      }
    }
    vis[a] = 0;
  }
}
int MAIN() {
  scanf("%d%d", &n, &m);
  for (int i = 0; i <= n; i++)
    for (int j = 0; j <= n; j++) go[i][j] = 1;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      go[j + 1][j + 1] = 0;
      go[j + 1][0] = 0;
      scanf("%d", &num[i][j]);
      for (int k = 0; k < j; k++) {
        go[num[i][j]][num[i][k]] = 0;
      }
    }
  }
  spfa();
  int ans = 0;
  for (int j = 0; j <= n; j++) {
    ans = max(ans, dis[j]);
  }
  printf("%d\n", ans);
  return 0;
}
int main() {
  int cases;
  MAIN();
  return 0;
}
