#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
const int M = N * N;
int n, m, pos[N];
int dis[N], flag[N][N];
char *p1, *p2, buf[100000];
int rd() {
  int x = 0, f = 1;
  char c =
      (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
           ? EOF
           : *p1++);
  while (c < 48) {
    if (c == '-') {
      f = -1;
    }
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  while (c > 47) {
    x = (((x << 2) + x) << 1) + (c ^ 48);
    c = (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, 100000, stdin), p1 == p2)
             ? EOF
             : *p1++);
  }
  return x * f;
}
int tot, head[N], nxt[M], to[M];
void add(int x, int y) {
  to[++tot] = y;
  nxt[tot] = head[x];
  head[x] = tot;
}
int dfs(int x) {
  if (~dis[x]) {
    return dis[x];
  }
  dis[x] = 0;
  for (int i = head[x]; i; i = nxt[i]) {
    dis[x] = max(dis[x], dfs(to[i]));
  }
  return (++dis[x]);
}
int main() {
  n = rd(), m = rd();
  memset(dis, -1, sizeof dis);
  memset(flag, 1, sizeof flag);
  for (int i = 1; i <= m; i++) {
    for (int j = 1; j <= n; j++) {
      int x = rd();
      pos[x] = j;
    }
    for (int j = 1; j <= n; j++) {
      for (int k = 1; k <= n; k++) {
        flag[j][k] &= (pos[j] < pos[k]);
      }
    }
  }
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (flag[i][j] == 1) {
        add(i, j);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dfs(i));
  }
  printf("%d", ans);
  return 0;
}
