#include <bits/stdc++.h>
using namespace std;
int n, k;
int a[1000 + 10][1000 + 10];
int b[1000 + 10][1000 + 10];
bool check(int x, int y) {
  for (int i = 2; i <= k; i++) {
    if (b[i][x] > b[i][y]) return false;
  }
  return true;
}
int main() {
  ios::sync_with_stdio(1), cin.tie(0);
  cin >> n >> k;
  for (int i = 1; i <= k; i++) {
    for (int j = 1; j <= n; j++) {
      cin >> a[i][j];
      b[i][a[i][j]] = j;
    }
  }
  int dp[1000 + 10];
  for (int i = 0; i <= n; i++) dp[i] = 1;
  for (int i = 1; i <= n; i++) {
    for (int j = i + 1; j <= n; j++) {
      if (check(a[1][i], a[1][j])) {
        dp[j] = max(dp[i] + 1, dp[j]);
      }
    }
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, dp[i]);
  }
  cout << ans << endl;
  return 0;
}
