#include <bits/stdc++.h>
using namespace std;
int n, m, arr[1003][1003];
int yer[6][1003], k;
int i, j;
int dp[1003];
vector<int> v[1003];
int f(int x) {
  if (dp[x]) return dp[x];
  for (int i = 0; i < v[x].size(); i++) dp[x] = max(dp[x], f(v[x][i]) + 1);
  return dp[x];
}
int main() {
  cin >> m >> n;
  for (i = 1; i <= n; i++)
    for (j = 1; j <= m; j++) {
      cin >> arr[i][j];
      yer[i][arr[i][j]] = j;
    }
  for (i = 1; i <= m; i++)
    for (j = 1; j <= m; j++)
      if (i != j) {
        int t = 0;
        for (k = 1; k <= n; k++)
          if (yer[k][i] > yer[k][j]) t = 1;
        if (!t) v[i].push_back(j);
      }
  int mx = 0;
  for (i = 1; i <= m; i++) mx = max(f(i), mx);
  cout << mx + 1 << endl;
}
