#include <bits/stdc++.h>
using namespace std;
int a[5][1005];
int pos[5][1005];
vector<int> g[1005];
int indegree[1005];
int dist[1005];
bool isBefore(int row, int v1, int v2) { return pos[row][v1] < pos[row][v2]; }
int main(int argc, const char *argv[]) {
  int n, k;
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      scanf("%d", &a[i][j]);
      pos[i][a[i][j]] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      int v1 = a[0][i], v2 = a[0][j];
      bool ok = true;
      for (int r = 1; r < k; r++) {
        if (!isBefore(r, v1, v2)) {
          ok = false;
          break;
        }
      }
      if (ok) {
        g[v1].push_back(v2);
        indegree[v2]++;
      }
    }
  }
  queue<int> q;
  for (int i = 1; i <= n; i++) {
    if (indegree[i] == 0) {
      q.push(i);
      dist[i] = 1;
    }
  }
  int maxi = 1;
  while (!q.empty()) {
    int v = q.front();
    q.pop();
    for (auto x : g[v]) {
      if (dist[x] < dist[v] + 1) {
        dist[x] = dist[v] + 1;
        maxi = max(dist[x], maxi);
      }
      indegree[x]--;
      if (indegree[x] == 0) {
        q.push(x);
      }
    }
  }
  printf("%d\n", maxi);
  return 0;
}
