#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 100;
int a[6][N];
int n, k;
int pos[10][N];
vector<int> g[N];
int ans;
bool was[N];
int len[N];
void dfs(int v) {
  was[v] = 1;
  len[v] = 1;
  for (int to : g[v]) {
    if (was[to] == 0) dfs(to);
    len[v] = max(len[v], len[to] + 1);
  }
  ans = max(ans, len[v]);
}
int main() {
  cin >> n >> k;
  for (int i = 1; i <= k; ++i) {
    for (int j = 1; j <= n; ++j) {
      cin >> a[i][j];
      pos[i][a[i][j]] = j;
    }
  }
  for (int i = 1; i <= n; ++i) {
    for (int j = 1; j <= n; ++j) {
      bool ch = 1;
      for (int z = 1; z <= k; ++z) {
        if (pos[z][i] >= pos[z][j]) {
          ch = 0;
        }
      }
      if (ch) {
        g[i].push_back(j);
      }
    }
  }
  for (int i = 1; i <= n; ++i) {
    if (was[i] == 0) {
      dfs(i);
    }
  }
  cout << ans;
}
