#include <bits/stdc++.h>
using namespace std;
int dp[1009];
int pos[6][1009];
vector<int> g[1009];
int n, k, aux;
bool check(int p, int q) {
  for (int i = 0; i < k; i++) {
    if (pos[i][p] >= pos[i][q]) return false;
  }
  return true;
}
void crear() {
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (check(i, j)) g[i].push_back(j);
}
int solve(int u) {
  if (dp[u] != -1) return dp[u];
  int &ret = dp[u];
  ret = 0;
  for (__typeof((g[u]).begin()) it = (g[u]).begin(); it != (g[u]).end(); it++) {
    ret = max(ret, 1 + solve(*it));
  }
  return ret;
}
int main(int argc, char const *argv[]) {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n >> k;
  for (int i = (int)(0); i < (int)(k); i++) {
    for (int j = (int)(0); j < (int)(n); j++) {
      cin >> aux;
      pos[i][aux] = j;
    }
  }
  for (int i = 1; i <= n; i++) g[0].push_back(i);
  crear();
  memset(dp, -1, sizeof(dp));
  cout << solve(0) << endl;
  return 0;
}
