#include <bits/stdc++.h>
using namespace std;
const int maxn = 2000, maxk = 6;
int per[maxk][maxn];
int n, K;
struct edge {
  int v, nxt;
} E[maxn * maxn];
int p[maxn], eid;
int indegree[maxn];
void init() {
  memset(p, -1, sizeof p);
  eid = 0;
}
void insert(int u, int v) {
  indegree[v]++;
  E[eid].v = v;
  E[eid].nxt = p[u];
  p[u] = eid++;
}
bool vis[maxn];
int pos[maxk][maxn];
int ans = 0;
int nowans = 0;
void dfs(int u, int step) {
  vis[u] = true;
  nowans = max(nowans, step);
  for (int i = p[u]; ~i; i = E[i].nxt) {
    int v = E[i].v;
    if (!vis[v]) {
      dfs(v, step + 1);
    }
  }
  vis[u] = false;
}
int cnt = 0;
int topo() {
  int maxdep = 0;
  queue<pair<int, int> > q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(make_pair(i, 1));
    }
  }
  while (!q.empty()) {
    pair<int, int> now = q.front();
    maxdep = max(maxdep, now.second);
    q.pop();
    for (int i = p[now.first]; ~i; i = E[i].nxt) {
      int v = E[i].v;
      indegree[v]--;
      if (indegree[v] == 0) {
        q.push(make_pair(v, now.second + 1));
      }
    }
  }
  return maxdep;
}
int main() {
  scanf("%d%d", &n, &K);
  for (int i = 1; i <= K; i++)
    for (int j = 1; j <= n; j++) {
      scanf("%d", &per[i][j]);
      pos[i][per[i][j]] = j;
    }
  init();
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j) continue;
      bool flag = true;
      for (int k = 1; k <= K; k++) {
        if (pos[k][i] > pos[k][j]) {
          flag = false;
          break;
        }
      }
      if (flag) insert(i, j);
    }
  }
  cout << topo() << endl;
  return 0;
}
