#include <bits/stdc++.h>
using namespace std;
int abs(int x) {
  if (x < 0) return -x;
  return x;
}
int addmod(int v1, int v2) {
  int v3 = v1 + v2;
  if (v3 >= 1000000007) v3 -= 1000000007;
  return v3;
}
int cc[100];
int a[10005][10], pos[10002][10], vis[100004];
int dp[10005];
int mark[100005];
int main() {
  int n, k;
  scanf("%d", &n), scanf("%d", &k);
  for (int i = (0); i < (k); i++) {
    for (int j = (0); j < (n); j++) {
      scanf("%d", &a[j][i]);
      a[j][i]--;
    }
  }
  memset(pos, -1, sizeof(pos));
  for (int i = (0); i < (n); i++) {
    for (int j = (0); j < (k); j++) {
      int co = 0;
      for (int l = (0); l < (k); l++) {
        if (a[i][j] == a[i][l]) co++;
      }
      if (vis[a[i][j]] + co == k) {
        dp[a[i][j]] = 1;
        for (int f = (0); f < (n); f++) {
          if (f == a[i][j]) continue;
          if (vis[f] == k) {
            bool poss = true;
            for (int l = (0); l < (k); l++) {
              if (pos[a[i][j]][l] == -1) continue;
              if (pos[a[i][j]][l] < pos[f][l]) poss = false;
            }
            if (poss) {
              dp[a[i][j]] = max(dp[a[i][j]], dp[f] + 1);
            }
          }
        }
      }
    }
    for (int j = (0); j < (k); j++) {
      vis[a[i][j]]++;
      pos[a[i][j]][j] = i;
    }
  }
  int ans = 0;
  for (int i = (0); i < (n); i++) ans = max(ans, dp[i]);
  cout << (ans), cout << (endl);
}
