#include <bits/stdc++.h>
using namespace std;
const int N = 1000 + 10;
int d[6][N], pos[6][N], cnt[N], f[N];
vector<int> vec;
int n, k, ans;
int main() {
  scanf("%d%d", &n, &k);
  for (int i = 0; i < k; i++)
    for (int j = 0; j < n; j++) scanf("%d", &d[i][j]);
  for (int i = 0; i < n; i++)
    for (int j = 0; j < k; j++) {
      int cur = d[j][i];
      pos[j][cur] = i;
      cnt[cur]++;
      if (cnt[cur] == k) {
        if (vec.size() == 0)
          f[cur] = 1;
        else
          for (int kk = 0; kk < vec.size(); kk++) {
            bool flag = false;
            for (int l = 0; l < k; l++)
              if (pos[l][cur] < pos[l][vec[kk]]) {
                flag = true;
                break;
              }
            if (!flag)
              f[cur] = max(f[cur], f[vec[kk]] + 1);
            else
              f[cur] = max(f[cur], 1);
          }
        ans = max(ans, f[cur]);
        vec.push_back(d[j][i]);
      }
    }
  cout << ans << endl;
  return 0;
}
