#include <bits/stdc++.h>
using namespace std;
const int K = 7;
const int N = 2005;
int a[K][N];
vector<int> s[N];
int dp[N], n, k;
bool can_be_next(const vector<int>& a, const vector<int>& b) {
  for (int i = 0; i < (int)a.size(); i++) {
    if (b[i] <= a[i]) {
      return false;
    }
  }
  return true;
}
int main() {
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    for (int j = 0; j < n; j++) {
      int temp;
      cin >> temp;
      --temp;
      if (i == 0) {
        a[i][j] = temp;
      } else {
        a[i][temp] = j;
      }
    }
  }
  for (int i = 0; i < n; i++) {
    s[i] = vector<int>(k - 1);
    for (int j = 1; j < k; ++j) s[i][j - 1] = a[j][a[0][i]];
  }
  int res = 0;
  for (int i = 0; i < n; i++) {
    dp[i] = 1;
    for (int j = 0; j < i; j++) {
      if (can_be_next(s[j], s[i])) {
        dp[i] = max(dp[j] + 1, dp[i]);
      }
    }
    res = max(res, dp[i]);
  }
  cout << res;
  return 0;
}
