#include <bits/stdc++.h>
using namespace std;
const int N = 2e3 + 5;
const int mod = 1e9 + 7;
int n, k, ar[5][N], len[N];
vector<int> pos[N];
int solve() {
  for (int i = 1; i <= n; i++) {
    int mx = -1;
    for (int j = 0; j < i; j++) {
      bool is = 1;
      for (int a = 0; a < k; a++)
        if (pos[ar[0][i]][a] < pos[ar[0][j]][a]) is = 0;
      if (is && mx < len[j]) mx = len[j];
    }
    len[i] = mx + 1;
  }
  int mx = 0;
  for (int i = 1; i <= n; i++) mx = max(mx, len[i]);
  return mx;
}
int main() {
  int x;
  cin >> n >> k;
  for (int i = 0; i < k; i++) {
    pos[0].push_back(0);
    for (int j = 1; j <= n; j++) {
      cin >> ar[i][j];
      pos[ar[i][j]].push_back(j);
    }
  }
  cout << solve();
  return 0;
}
