#include <bits/stdc++.h>
using namespace std;
inline long long read() {
  long long x = 0;
  char ch = getchar(), w = 1;
  while (ch < '0' || ch > '9') {
    if (ch == '-') w = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * w;
}
void write(long long x) {
  if (x < 0) putchar('-'), x = -x;
  if (x > 9) write(x / 10);
  putchar(x % 10 + '0');
}
inline void writeln(long long x) {
  write(x);
  puts("");
}
const int N = 2100;
int n, m, a[N][N];
int b[N][N], f[N];
inline bool check(int x, int y) {
  for (int i = 2; i <= m; ++i)
    if (b[i][x] > b[i][y]) return 0;
  return 1;
}
int main() {
  n = read(), m = read();
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= n; ++j) a[i][j] = read(), b[i][a[i][j]] = j;
  int ans = 0;
  for (int i = 1; i <= n; ++i) {
    f[i] = 1;
    for (int j = 1; j < i; ++j)
      if (check(a[1][j], a[1][i])) f[i] = max(f[i], f[j] + 1);
    ans = max(ans, f[i]);
  }
  writeln(ans);
  return 0;
}
