#include <bits/stdc++.h>
using namespace std;
int arr[10][1010];
int pos[10][1010];
vector<int> G[1010];
int vis[1010], ans = 0;
int dfs(int node) {
  if (vis[node] != -1) return vis[node];
  vis[node] = 1;
  int size = G[node].size();
  for (int i = 0; i < size; i++) {
    vis[node] = max(vis[node], 1 + dfs(G[node][i]));
  }
  ans = max(ans, vis[node]);
  return vis[node];
}
int main() {
  cin.sync_with_stdio(false);
  int N, K;
  cin >> N >> K;
  for (int i = 1; i < K + 1; i++) {
    for (int j = 1; j < N + 1; j++) {
      cin >> arr[i][j];
      pos[i][arr[i][j]] = j;
    }
  }
  for (int i = 0; i < N + 1; i++) G[i].clear();
  for (int i = 1; i < N + 1; i++) {
    for (int j = 1; j < N + 1; j++) {
      if (i == j) continue;
      bool OK = 1;
      for (int k = 1; k < K + 1; k++) {
        if (pos[k][i] >= pos[k][j]) {
          OK = 0;
          break;
        }
      }
      if (OK) {
        G[i].push_back(j);
      }
    }
  }
  memset(vis, -1, sizeof(vis));
  for (int i = 1; i < N + 1; i++) {
    if (vis[i] == -1) dfs(i);
  }
  cout << ans << endl;
}
