#include <bits/stdc++.h>
using namespace std;
const int maxn = 1010;
int n, m, ans;
int a[20][maxn], pos[20][maxn], dp[maxn];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%d", &a[i][j]);
      pos[i][a[i][j]] = j;
    }
  for (int i = 1; i <= n; i++) dp[i] = 1;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j < i; j++) {
      int x = a[1][i];
      int y = a[1][j];
      int flag = 1;
      for (int t = 1; t <= m && flag; t++)
        if (pos[t][x] < pos[t][y]) flag = 0;
      if (flag) dp[x] = max(dp[x], dp[y] + 1);
    }
  ans = 0;
  for (int i = 1; i <= n; i++) ans = max(ans, dp[i]);
  printf("%d", ans);
  return 0;
}
