#include <bits/stdc++.h>
using namespace std;
const int N = 1005;
const int inf = 2000000000;
int q, ans, fl, last, now, n, k, i, j, a[N][N], pos[N][N], dp[N];
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(0);
  cin >> n >> k;
  for (i = 1; i <= k; i++)
    for (j = 1; j <= n; j++) {
      cin >> a[i][j];
      pos[i][a[i][j]] = j;
    }
  for (i = 1; i <= n; i++) {
    now = a[1][i];
    dp[now] = 1;
    for (j = 1; j < i; j++) {
      fl = 1;
      last = a[1][j];
      for (q = 2; q <= k; q++)
        if (pos[q][last] > pos[q][now]) {
          fl = 0;
          break;
        }
      if (fl) dp[now] = max(dp[now], dp[last] + 1);
    }
  }
  for (i = 1; i <= n; i++) ans = max(ans, dp[i]);
  cout << ans;
}
