#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1001;
vector<int> g[N];
vector<int> topsort;
bool used[N];
void Dfs(int from) {
  used[from] = true;
  for (int v : g[from]) {
    if (used[v]) continue;
    Dfs(v);
  }
  topsort.push_back(from);
}
void setmax(int& a, int b) {
  if (a < b) a = b;
}
int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int n, m;
  cin >> n >> m;
  vector<vector<int>> arr(m, vector<int>(n));
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int val;
      cin >> val;
      val--;
      arr[i][val] = j;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) continue;
      bool edge = true;
      for (int k = 0; k < m; k++) {
        edge &= arr[k][i] < arr[k][j];
      }
      if (edge) {
        g[i].push_back(j);
      }
    }
  }
  for (int i = 0; i < n; i++) {
    if (!used[i]) {
      Dfs(i);
    }
  }
  vector<int> dp(n, 1);
  for (int from : topsort) {
    for (int to : g[from]) {
      setmax(dp[from], dp[to] + 1);
    }
  }
  cout << *max_element((dp).begin(), (dp).end()) << endl;
  ;
}
