#include <bits/stdc++.h>
using namespace std;
const int maxn = 1005;
const int maxm = 1;
int n, k, ans;
int v[maxn][maxn], pos[maxn][maxn], f[maxn];
namespace Read {
template <typename T>
inline void read(T& x) {
  x = 0;
  T f = 1;
  char ch = getchar();
  while (!isdigit(ch)) {
    if (ch == '-') f = -1;
    ch = getchar();
  }
  while (isdigit(ch)) {
    x = x * 10 + (ch ^ 48);
    ch = getchar();
  }
  x *= f;
}
template <typename T, typename... Args>
inline void read(T& t, Args&... args) {
  read(t);
  read(args...);
}
}  // namespace Read
using namespace Read;
signed main() {
  read(n, k);
  for (register int i = 1; i <= k; i++) {
    for (register int j = 1; j <= n; j++) {
      read(v[i][j]);
      pos[i][v[i][j]] = j;
    }
  }
  for (register int i = 1; i <= n; i++) {
    f[i] = 1;
    for (register int j = 1; j < i; j++) {
      bool flag = true;
      for (register int t = 2; t <= k && flag; t++) {
        if (pos[t][v[1][j]] > pos[t][v[1][i]]) flag = false;
      }
      if (flag) f[i] = max(f[i], f[j] + 1);
    }
    ans = max(ans, f[i]);
  }
  printf("%d\n", ans);
  return 0;
}
