#include <bits/stdc++.h>
using namespace std;
const int N = 1000;
int n, k;
int a[N];
int pos[5][N];
int dp[N];
int main() {
  scanf("%d %d", &n, &k);
  for (int j = 0; j < k; ++j) {
    for (int i = 0; i < n; ++i) {
      int foo;
      scanf("%d", &foo);
      --foo;
      pos[j][foo] = i;
      if (j == 0) a[i] = foo;
    }
  }
  for (int i = n - 1; i >= 0; --i) {
    int &res = dp[i];
    res = 0;
    for (int next = i + 1; next < n; ++next) {
      bool ok = true;
      for (int j = 0; j < k; ++j) {
        if (pos[j][a[i]] >= pos[j][a[next]]) {
          ok = false;
          break;
        }
      }
      if (ok) res = max(res, dp[next] + 1);
    }
  }
  int ans = 0;
  for (int i = 0; i < n; ++i) {
    ans = max(ans, dp[i] + 1);
  }
  printf("%d\n", ans);
  return 0;
}
