#include <bits/stdc++.h>
using namespace std;
const int MAXN = 1000 + 10;
int a[10][MAXN];
int b[10][MAXN];
int dp[MAXN] = {};
int n, m;
bool check(int x, int y) {
  for (int i = 0; i < m; i++) {
    if (b[i][x] < b[i][y]) return 0;
  }
  return 1;
}
int main() {
  scanf("%d%d", &n, &m);
  int ans = 0;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
      b[i][a[i][j]] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < i; j++) {
      if (check(a[0][i], a[0][j])) dp[i] = max(dp[i], dp[j] + 1);
    }
    ans = max(ans, dp[i]);
  }
  printf("%d\n", ans + 1);
}
