#include <bits/stdc++.h>
using namespace std;
const int M = 10005;
int dp[M];
int pos[8][M];
int n, k;
int a[8][M];
bool check(int x, int y) {
  for (int i = 2; i <= k; i++) {
    if (pos[i][x] > pos[i][y]) return false;
  }
  return true;
}
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 1; i <= k; 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 = i + 1; j <= n; j++) {
      if (check(a[1][i], a[1][j])) dp[j] = max(dp[i] + 1, dp[j]);
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(dp[i], ans);
  }
  printf("%d\n", ans);
  return 0;
}
