#include <bits/stdc++.h>
using namespace std;
const int oo = 1e9 + 7;
const int maxn = 1007;
int n, k, x, pos[10][maxn], g;
bool avail[maxn];
int topo[maxn], rev[maxn], f[maxn];
vector<int> adj[maxn];
inline void read(int &x) {
  register int c = getchar();
  x = 0;
  int neg = 0;
  for (; ((c < 48 || c > 57) && c != '-'); c = getchar())
    ;
  if (c == '-') {
    neg = 1;
    c = getchar();
  }
  for (; c > 47 && c < 58; c = getchar()) {
    x = (x << 1) + (x << 3) + c - 48;
  }
  if (neg) x = -x;
}
inline void writeln(long long x) {
  char buffor[21];
  register int i = 0;
  int neg = 0;
  if (x < 0) {
    neg = 1;
    x = -x;
  }
  do {
    buffor[i++] = (x % 10) + '0';
    x /= 10;
  } while (x);
  i--;
  if (neg) putchar('-');
  while (i >= 0) putchar(buffor[i--]);
  putchar('\n');
}
void nhap() {
  read(n);
  read(k);
  for (int i = (1), _b = (k); i <= _b; ++i)
    for (int j = (1), _b = (n); j <= _b; ++j) {
      read(x);
      pos[i][x] = j;
    }
}
void Topo(int u) {
  avail[u] = false;
  if (adj[u].size())
    for (int i = (0), _b = (adj[u].size() - 1); i <= _b; ++i) {
      int v = adj[u][i];
      if (avail[v]) Topo(v);
    }
  rev[g] = u;
  topo[u] = g--;
}
inline int LongestPath() {
  memset(avail, true, sizeof(avail));
  g = n;
  for (int i = (1), _b = (n); i <= _b; ++i)
    if (avail[i]) Topo(i);
  fill(f, f + n + 1, 1);
  for (int i = (1), _b = (n); i <= _b; ++i) {
    int u = rev[i];
    if (adj[u].size())
      for (int j = (0), _b = (adj[u].size() - 1); j <= _b; ++j) {
        int v = topo[adj[u][j]];
        f[v] = max(f[v], f[i] + 1);
      }
  }
  return *max_element(f + 1, f + n + 1);
}
void solve() {
  for (int i = (1), _b = (n); i <= _b; ++i)
    for (int j = (1), _b = (n); j <= _b; ++j)
      if (i != j) {
        bool flag = true;
        for (int l = (1), _b = (k); l <= _b; ++l) {
          if (pos[l][i] > pos[l][j]) {
            flag = false;
            break;
          }
        }
        if (flag) adj[i].push_back(j);
      }
}
void print() { cout << LongestPath(); }
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  nhap();
  solve();
  print();
}
