#include <bits/stdc++.h>
using namespace std;
int a[1000][5], g[1005][1005];
int main() {
  int n, m, x;
  scanf("%d%d", &n, &m);
  for (int i = 0; i < m; i++)
    for (int j = 0; j < n; j++) {
      scanf("%d", &x);
      a[x - 1][i] = j;
    }
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      bool r = true;
      for (int k = 0; k < m; k++) r &= (a[i][k] > a[j][k]);
      if (r)
        g[i][j] = 1;
      else
        g[i][j] = -10000;
    }
  for (int k = 0; k < n; k++)
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++) g[i][j] = max(g[i][j], g[i][k] + g[k][j]);
  int mx = 0;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) mx = max(mx, g[i][j]);
  cout << mx + 1 << endl;
  return 0;
}
