#include <bits/stdc++.h>
using namespace std;
int pos[6][1010], Best[1010], maxi;
bool visited[1010];
vector<int> adj[1010];
void dfs(int src) {
  visited[src] = 1;
  for (int A = 0; A < adj[src].size(); A++) {
    int child = adj[src][A];
    if (!visited[child]) {
      visited[child] = 1;
      Best[child] = Best[src] + 1;
      dfs(child);
    }
  }
  maxi = max(maxi, Best[src]);
}
void dfs2(int Node) {
  visited[Node] = 1;
  for (int i = 0; i < adj[Node].size(); ++i) {
    if (!visited[adj[Node][i]]) dfs2(adj[Node][i]);
    Best[Node] = max(Best[Node], Best[adj[Node][i]] + 1);
  }
  Best[Node] = max(Best[Node], 1);
  maxi = max(maxi, Best[Node]);
}
int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int row, collumn, temp;
  cin >> collumn >> row;
  for (int A = 1; A <= row; A++) {
    for (int B = 1; B <= collumn; B++) {
      cin >> temp;
      pos[A][temp] = B;
    }
  }
  bool k;
  for (int A = 1; A <= collumn; A++) {
    for (int B = 1; B <= collumn; B++) {
      if (A == B) continue;
      k = true;
      for (int C = 1; C <= row; C++) {
        if (pos[C][A] >= pos[C][B]) {
          k = false;
          break;
        }
      }
      if (k) adj[A].push_back(B);
    }
  }
  memset((visited), 0, sizeof(visited));
  maxi = 0;
  for (int A = 1; A <= collumn; A++) {
    if (!visited[A]) {
      dfs2(A);
    }
  }
  cout << maxi << endl;
}
