#include <bits/stdc++.h>
using namespace std;
const int MAXN = 10 + 1000;
const int INF = int(1e9);
int n, m;
int pos[10][MAXN], a[10][MAXN], f[MAXN];
int check(int a, int b) {
  for (int i = (1), _b = (m); i <= m; ++i)
    if (pos[i][a] > pos[i][b]) return 0;
  return 1;
}
int main() {
  cin >> n >> m;
  for (int i = (1), _b = (m); i <= m; ++i)
    for (int j = (1), _b = (n); j <= n; ++j) {
      cin >> a[i][j];
      pos[i][a[i][j]] = j;
    }
  for (int i = (1), _b = (n); i <= n; ++i) f[i] = 1;
  int res = 1;
  for (int i = (1), _b = (n); i <= n; ++i)
    for (int j = (1), _b = (i - 1); j <= i - 1; ++j)
      if (check(a[1][j], a[1][i])) {
        f[i] = max(f[i], f[j] + 1);
        res = max(res, f[i]);
      }
  printf("%d", res);
  return 0;
}
