#include <bits/stdc++.h>
using namespace std;
const int MAX = 1050;
int dp[MAX];
vector<int> pos[MAX];
int val[MAX];
int main() {
  int n, k;
  int v;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      cin >> v;
      val[j] = v;
      pos[v].push_back((j));
    }
  }
  fill(dp, dp + n + 2, 1);
  for (int i = n - 1; i > -1; i--) {
    v = val[i];
    for (int j = 1; j <= n; j++) {
      if (j == v) continue;
      bool flag = true;
      for (int l = 0; l < k; l++) {
        if (pos[v][l] > pos[j][l]) {
          flag = false;
          break;
        }
      }
      if (flag) {
        dp[v] = max(dp[v], dp[j] + 1);
      }
    }
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) mx = max(mx, dp[i]);
  cout << mx << endl;
  return 0;
}
