#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const double eps = 1e-8;
int read() {
  int x = 0, f = 0;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = 1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return f ? -x : x;
}
int n, m, pos[7][N], dp[N];
vector<int> G[N];
int dfs(int x) {
  if (dp[x]) return dp[x];
  dp[x] = 1;
  for (int i = 0; i < G[x].size(); i++) {
    dp[x] = max(dfs(G[x][i]) + 1, dp[x]);
  }
  return dp[x];
}
int main() {
  n = read();
  m = read();
  for (int j = 1; j <= m; j++) {
    for (int i = 1; i <= n; i++) {
      int a = read();
      pos[j][a] = i;
    }
  }
  for (int j = 1; j <= n; j++) {
    for (int k = 1; k <= n; k++) {
      for (int i = 1; i <= m; i++) {
        if (pos[i][j] >= pos[i][k]) break;
        if (i == m) {
          G[j].push_back(k);
        }
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  printf("%d\n", ans);
  return 0;
}
