#include <bits/stdc++.h>
using namespace std;
const double eps = 1e-10;
const double PI = atan(1.0) * 4;
const int inf = ~0U >> 1;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, 1, 0, -1};
template <class T>
bool chmin(T& a, const T& b) {
  return b < a ? a = b, 1 : 0;
}
template <class T>
bool chmax(T& a, const T& b) {
  return b > a ? a = b, 1 : 0;
}
template <class T>
int sgn(T first) {
  return (first > eps) - (first < -eps);
}
const int maxn = 1005;
int pos[maxn][10];
int ru[maxn], ans, d[maxn];
vector<int> pat[maxn];
void dfs(int u) {
  chmax(ans, d[u]);
  for (auto v : pat[u]) {
    if (d[v] >= d[u] + 1) continue;
    d[v] = d[u] + 1;
    dfs(v);
  }
}
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  for (int i = (0); i <= (int)(m - 1); i++) {
    int t;
    for (int j = (0); j <= (int)(n - 1); j++) {
      scanf("%d", &t);
      pos[--t][i] = j;
    }
  }
  memset(ru, 0, sizeof(ru));
  for (int i = (0); i <= (int)(n - 1); i++) pat[i].clear();
  for (int i = (0); i <= (int)(n - 1); i++)
    for (int j = (i + 1); j <= (int)(n - 1); j++) {
      int v = 0;
      for (int k = (0); k <= (int)(m - 1); k++)
        v |= (1 << (pos[i][k] < pos[j][k]));
      if (v == 3) {
        continue;
      } else if (v == 1) {
        pat[j].push_back(i);
        ++ru[i];
      } else {
        pat[i].push_back(j);
        ++ru[j];
      }
    }
  ans = 1;
  memset(d, 0, sizeof(d));
  for (int i = (0); i <= (int)(n - 1); i++)
    if (!ru[i]) d[i] = 1, dfs(i);
  printf("%d\n", ans);
  return 0;
}
