#include <bits/stdc++.h>
using namespace std;
int dp[1001];
int v[5][1001];
int n, k;
int main() {
  cin >> n >> k;
  for (int j = 1; j <= n; ++j) cin >> v[0][j];
  int a;
  for (int i = 1; i < k; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a;
      v[i][a] = j;
    }
  }
  int maxtot = 1;
  dp[1] = 1;
  for (int i = 2; i <= n; ++i) {
    int maxx = 0;
    int va = v[0][i];
    for (int j = 1; j < i; ++j) {
      if (dp[j] <= maxx) continue;
      int vb = v[0][j];
      bool same = true;
      for (int z = 1; z < k; ++z)
        if (v[z][va] < v[z][vb]) {
          same = false;
          break;
        }
      if (same) maxx = max(maxx, dp[j]);
    }
    dp[i] = maxx + 1;
    if (dp[i] > maxtot) maxtot = dp[i];
  }
  cout << maxtot << endl;
  return 0;
}
