#include <bits/stdc++.h>
using namespace std;
const int MOD = 1e9 + 7;
const int N = 1e3 + 10;
int n, k;
int V[10][N];
vector<int> adj[N];
int pos[10][N];
int c;
int vis[N];
int dist[N];
void dfs(int u) {
  vis[u] = 1;
  dist[u] = 1;
  for (auto s : adj[u]) {
    if (!vis[s]) {
      dfs(s);
    }
    dist[u] = max(dist[u], dist[s] + 1);
  }
  c = max(dist[u], c);
}
int32_t main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> V[i][j];
      pos[i][V[i][j]] = j;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      if (i == j) {
        continue;
      }
      int valid = 1;
      for (int l = 1; l <= k; ++l) {
        valid &= (pos[l][i] < pos[l][j]);
      }
      if (valid) {
        adj[i].push_back(j);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    42;
    if (!vis[i]) {
      dfs(i);
    }
    42;
  }
  cout << c;
  return 0;
}
