#include <bits/stdc++.h>
using namespace std;
typedef pair<long long, long long> ll;
typedef vector<long long> vl;
typedef vector<ll> vll;
typedef vector<vl> vvl;
template <typename T>
ostream &operator<<(ostream &o, vector<T> v) {
  if (v.size() > 0) o << v[0];
  for (unsigned i = 1; i < v.size(); i++) o << " " << v[i];
  return o << " ";
}
template <typename U, typename V>
ostream &operator<<(ostream &o, pair<U, V> p) {
  return o << "(" << p.first << ", " << p.second << ") ";
}
template <typename T>
istream &operator>>(istream &in, vector<T> &v) {
  for (unsigned i = 0; i < v.size(); i++) in >> v[i];
  return in;
}
template <typename T>
istream &operator>>(istream &in, pair<T, T> &p) {
  in >> p.first;
  in >> p.second;
  return in;
}
struct custom_hash {
  static uint64_t splitmix64(uint64_t x) {
    x += 0x9e3779b97f4a7c15;
    x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9;
    x = (x ^ (x >> 27)) * 0x94d049bb133111eb;
    return x ^ (x >> 31);
  }
  size_t operator()(uint64_t x) const {
    static const uint64_t FIXED_RANDOM =
        chrono::steady_clock::now().time_since_epoch().count();
    return splitmix64(x + FIXED_RANDOM);
  }
};
vl g[500010], visited(500010, false), res(500010, 0);
void dfs(long long node) {
  visited[node] = true;
  for (auto &it : g[node]) {
    if (!visited[it]) dfs(it);
    res[node] = max(res[node], res[it] + 1);
  }
  res[node] = max(res[node], 1LL);
}
int main(int argc, char *argv[]) {
  long long n, k;
  cin >> n >> k;
  long long pos[k][n + 1];
  for (long long i = (0); i < (long long)k; i++)
    for (long long j = (1); j < (long long)n + 1; j++) {
      long long x;
      cin >> x;
      pos[i][x] = j;
    }
  for (long long i = (1); i < (long long)n + 1; i++)
    for (long long j = (1); j < (long long)n + 1; j++) {
      if (i == j) continue;
      bool valid = true;
      for (long long(t) = 0; (t) < (k); (t)++)
        if (pos[t][i] >= pos[t][j]) valid = false;
      if (valid) g[i].push_back((j));
    }
  for (long long i = (1); i < (long long)n + 1; i++)
    if (!visited[i]) dfs(i);
  cout << *max_element((res).begin(), (res).end());
  return 0;
}
