#include <bits/stdc++.h>
using namespace std;
int main() {
  int n, k;
  scanf("%d", &n);
  scanf("%d", &k);
  vector<int> v;
  int i, j;
  int dp[1002], pos[6][1002];
  for (i = 0; i < k; i++) {
    for (j = 0; j < n; j++) {
      int x;
      scanf("%d", &x);
      if (i == 0) {
        v.push_back(x);
      }
      pos[i][x] = j;
    }
  }
  dp[0] = 1;
  for (i = 1; i < n; i++) {
    dp[i] = 1;
  }
  for (i = 1; i < v.size(); i++) {
    for (j = 0; j < i; j++) {
      int f = 1;
      for (int l = 0; l < k; l++) {
        if (pos[l][v[j]] > pos[l][v[i]]) {
          f = 0;
          break;
        }
      }
      if (f == 1) {
        dp[i] = max(dp[i], dp[j] + 1);
      }
    }
  }
  int ans = 0;
  for (i = 0; i < n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << "\n";
}
