#include <bits/stdc++.h>
using namespace std;
long long mod = 1e9 + 7;
vector<long long int> dp(1005, -1);
long long pos[1005][1005];
long long n, k;
long long solve(long long st) {
  if (dp[st] != -1) return dp[st];
  long long lo = 0;
  for (long long int i = 1; i < n + 1; i++) {
    long long fl = 0;
    for (long long int j = 0; j < k; j++) {
      if (pos[j][st] >= pos[j][i]) {
        fl = 1;
        break;
      }
    }
    if (fl == 0) lo = max(lo, 1 + solve(i));
  }
  return dp[st] = lo;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  int TESTS = 1;
  while (TESTS--) {
    cin >> n >> k;
    for (long long int i = 0; i < k; i++) {
      for (long long int j = 0; j < n; j++) {
        long long u;
        cin >> u;
        pos[i][u] = j;
      }
    }
    long long ans = 0;
    for (long long int i = 1; i < n + 1; i++) {
      ans = max(ans, 1 + solve(i));
    }
    cout << ans << "\n";
  }
  return 0;
}
