#include <bits/stdc++.h>
using namespace std;
template <class t>
inline t read(t &x) {
  char c = getchar();
  bool f = 0;
  x = 0;
  while (!isdigit(c)) f |= c == '-', c = getchar();
  while (isdigit(c)) x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  if (f) x = -x;
  return x;
}
template <class t>
inline void write(t x) {
  if (x < 0)
    putchar('-'), write(-x);
  else {
    if (x > 9) write(x / 10);
    putchar('0' + x % 10);
  }
}
const int N = 2005;
int n, m, ans, f[N], a[N], b[N][N], du[N], h[N], en;
queue<int> q;
struct edge {
  int n, v;
} e[N * N >> 1];
void add(int x, int y) {
  e[++en] = (edge){h[x], y};
  h[x] = en;
}
signed main() {
  read(m);
  read(n);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++) {
      read(a[j]);
      for (int k = 1; k < j; k++) b[a[k]][a[j]] = 1;
    }
  for (int i = 1; i <= m; i++)
    for (int j = 1; j < i; j++)
      if (b[i][j] ^ b[j][i]) {
        if (b[i][j])
          add(i, j), du[j]++;
        else
          add(j, i), du[i]++;
      }
  for (int i = 1; i <= m; i++)
    if (!du[i]) q.push(i);
  while (!q.empty()) {
    int x = q.front();
    q.pop();
    ans = max(ans, ++f[x]);
    for (int i = h[x]; i; i = e[i].n) {
      int y = e[i].v;
      f[y] = max(f[y], f[x]);
      if (!--du[y]) q.push(y);
    }
  }
  write(ans);
}
