#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
using namespace std;
const long long mod = (long long)1e9 + 7;
const long long MOD = (long long)998244353;
const long long INF = (long long)1e18;
void err(istream_iterator<string> it) {}
template <typename T, typename... Args>
void err(istream_iterator<string> it, T a, Args... args) {
  cerr << *it << " = " << a << endl;
  err(++it, args...);
}
const long long N = 1e3 + 5;
long long dp[N];
long long a[6][N], pos[N][N];
void solve() {
  long long n, k;
  cin >> n >> k;
  for (__typeof(k + 1) i = (1) - ((1) > (k + 1));
       i != (k + 1) - ((1) > (k + 1)); i += 1 - 2 * ((1) > (k + 1)))
    for (__typeof(n + 1) j = (1) - ((1) > (n + 1));
         j != (n + 1) - ((1) > (n + 1)); j += 1 - 2 * ((1) > (n + 1)))
      cin >> a[i][j], pos[i][a[i][j]] = j;
  for (long long i = 1; i <= n; ++i) {
    dp[i] = 1;
    for (long long j = 1; j < i; ++j) {
      bool f = 1;
      for (long long it = 1; it <= n; ++it) {
        if (pos[it][a[1][j]] > pos[it][a[1][i]]) {
          f = 0;
          break;
        }
      }
      if (f) dp[i] = max(dp[i], 1 + dp[j]);
    }
  }
  cout << *max_element(dp + 1, dp + n + 1);
}
int32_t main() {
  ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  ;
  solve();
  return 0;
}
