#include <bits/stdc++.h>
using namespace std;
const int N = 1e3 + 5;
int dp[N], pos[N][5], n, m, a;
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
  cin >> n >> m;
  for (int i = 0; i < m; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> a;
      pos[a][i] = j;
    }
  }
  for (int i = 1; i <= n; ++i) dp[i] = 1;
  int ans = 0;
  for (int turn = 0; turn < 100; ++turn) {
    for (int i = 1; i <= n; ++i) {
      for (int j = 1; j <= n; ++j) {
        if (i == j) continue;
        bool ok = true;
        for (int k = 0; k < m; ++k)
          if (pos[j][k] > pos[i][k]) ok = false;
        if (ok) dp[i] = max(dp[i], dp[j] + 1);
      }
      ans = max(ans, dp[i]);
    }
  }
  cout << ans;
  return 0;
}
