#include <bits/stdc++.h>
using namespace std;
int mak[2000][2000], l[2000][2000], dp[2000];
int ans, n, k;
;
bool check(int a, int b) {
  for (int i = 1; i <= k; i++) {
    if (mak[i][a] >= mak[i][b]) return 0;
  }
  return 1;
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
      int a;
      cin >> a;
      l[i][j] = a;
      mak[i][a] = j;
    }
  }
  fill(dp, dp + 2000, 1);
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j < i; j++) {
      if (check(l[1][j], l[1][i])) {
        dp[i] = max(dp[i], dp[j] + 1);
        ans = max(ans, dp[i]);
      }
    }
  }
  if (n == 1) {
    cout << 1;
    return 0;
  }
  cout << ans;
}
