#include <bits/stdc++.h>
#pragma comment(linker, "/STACK:256000000")
using namespace std;
const string LN = "\n";
const double eps = 1e-9;
const double pi = acos(-1.0);
const int INF = (int)2e9 + 7;
const long long LINF = (long long)9e18 + 7;
bool insert(int k, int x1, int x2) { return (k >= x1 && k <= x2); }
const int MAXN = 60007;
const long long MOD = 1e9 + 7;
int n, k, ans = 1;
int a[6][1007], pos[6][1007];
int path[1007];
bool was[1007];
vector<vector<int> > g;
void dfs(int v) {
  was[v] = true;
  for (int i = 0; i < g[v].size(); i++) {
    int to = g[v][i];
    if (!was[to]) dfs(to);
    path[v] = max(path[v], path[to] + 1);
  }
  path[v] = max(path[v], 1);
  ans = max(ans, path[v]);
}
const bool is_testing = 0;
int main() {
  if (is_testing) {
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
  }
  cin >> n >> k;
  g.resize(n + 2);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++) scanf("%d", &a[i][j]);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++) pos[i][a[i][j]] = j;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j) {
        bool f = true;
        for (int t = 0; t < k; t++)
          if (pos[t][i] > pos[t][j]) f = false;
        if (f) g[i].push_back(j);
      }
  for (int i = 1; i <= n; i++)
    if (!was[i]) dfs(i);
  cout << ans;
}
