#include <bits/stdc++.h>
using namespace std;
const int mx = 1e3 + 10;
int a[5][mx];
int ja[5][mx];
int dp[mx];
int n, k, ans = 0, b;
bool f(int x, int y) {
  for (int i = 0; i < k; i++)
    if (ja[i][x] > ja[i][y]) return 0;
  return 1;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      cin >> a[i][j];
      ja[i][a[i][j]] = j;
    }
  }
  for (int i = 0; i <= n; i++) {
    b = a[0][i];
    dp[b] = 1;
    for (int j = 1; j <= n; j++) {
      if (j != b && f(j, b)) {
        dp[b] = max(dp[j] + 1, dp[b]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans;
  return 0;
}
