#include <bits/stdc++.h>
using namespace std;
int n, m, a[11][1111], pos[11][1111], dp[1111];
int main() {
  scanf("%d%d", &n, &m);
  for (int i = 1; i <= m; ++i)
    for (int j = 1; j <= n; ++j) {
      scanf("%d", &a[i][j]);
      pos[i][a[i][j]] = j;
    }
  int res = 0;
  for (int i = 1; i <= n; ++i) {
    dp[a[1][i]] = 1;
    for (int j = 1; j <= i - 1; ++j) {
      bool isOk = true;
      for (int k = 2; k <= m; ++k)
        if (pos[k][a[1][i]] < pos[k][a[1][j]]) {
          isOk = false;
          break;
        }
      if (isOk) dp[a[1][i]] = max(dp[a[1][i]], dp[a[1][j]] + 1);
    }
    res = max(res, dp[a[1][i]]);
  }
  printf("%d", res);
  return 0;
}
