#include <bits/stdc++.h>
using namespace std;
int rint() {
  int x;
  if (scanf("%d", &x) != 1) return -1;
  return x;
}
string rstring() {
  static char buf[100000];
  if (scanf("%s", buf) != 1) return "";
  return buf;
}
int a[5][1000], loc[5][1001], L[1001];
int main() {
  int n = rint(), k = rint();
  for (int i(0); i < k; ++i)
    for (int j(0); j < n; ++j) a[i][j] = rint(), loc[i][a[i][j]] = j;
  for (int i(0); i < n; ++i) L[i + 1] = 0;
  for (int i(0); i < n; ++i) {
    int m = a[0][i];
    L[m] = 1;
    for (int t(1); t < n + 1; ++t) {
      bool b4 = true;
      for (int j(0); j < k; ++j) b4 &= loc[j][t] < loc[j][m];
      if (b4) L[m] = max(L[m], 1 + L[t]);
    }
  }
  int ans = 0;
  for (int i(0); i < n; ++i) ans = max(ans, L[i + 1]);
  cout << ans;
  return 0;
}
