#include <bits/stdc++.h>
using namespace std;
const int N = 1003;
int a[N][N], ans;
int n, m, x[5][N];
int dp[N];
int main(int argc, char **argv) {
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; ++i)
    for (int j = 0; j < n; ++j) {
      scanf("%d", &x[i][j]);
      a[i][x[i][j]] = j;
    }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < i; ++j) {
      bool ok = 1;
      for (int k = 0; k < m; ++k) ok &= a[k][x[0][j]] < a[k][x[0][i]];
      if (ok) dp[i] = max(dp[i], dp[j] + 1);
    }
  }
  for (int i = 1; i <= n; ++i) ans = max(ans, dp[i] + 1);
  cout << ans << endl;
  return 0;
}
