#include <bits/stdc++.h>
using namespace std;
inline long long modadd(long long n, long long m) {
  return ((n + m) % 1000000007 + 1000000007) % 1000000007;
}
inline long long modsub(long long n, long long m) {
  return ((n - m + 1000000007) % 1000000007 + 1000000007) % 1000000007;
}
inline long long modpro(long long n, long long m) {
  return ((n * m) % 1000000007 + 1000000007) % 1000000007;
}
inline unsigned long long int powe(long long first, long long second) {
  unsigned long long int res = 1;
  while (second > 0) {
    if (second & 1) res = res * first;
    second = second >> 1;
    first = first * first;
  }
  return res;
}
inline long long powmod(long long first, long long second) {
  long long res = 1;
  while (second > 0) {
    if (second & 1) res = modpro(res, first);
    second = second >> 1;
    first = modpro(first, first);
  }
  return res;
}
inline long long modadd3(long long first, long long second, long long z) {
  return modadd(modadd(first, second), z);
}
inline long long modadd4(long long first, long long second, long long z,
                         long long w) {
  return modadd(modadd(first, second), modadd(z, w));
}
template <typename T>
inline T max3(T first, T second, T z) {
  return max(max(first, second), z);
}
template <typename T>
inline T max4(T first, T second, T z, T w) {
  return max(max3(first, second, w), z);
}
template <typename T>
inline T min3(T first, T second, T z) {
  return min(min(first, second), z);
}
template <typename T>
inline T min4(T first, T second, T z, T w) {
  return min(min3(first, second, w), z);
}
template <typename T>
void printArr(T *arr, int n) {
  for (int i = 0; i < n; i++) {
    cout << arr[i] << " ";
  }
  cout << endl;
}
long long power(long long first, long long second, long long p) {
  long long res = 1;
  first = first % p;
  while (second > 0) {
    if (second & 1) res = (res * first) % p;
    second = second >> 1;
    first = (first * first) % p;
  }
  return res;
}
int modInverse(int n, long long p) { return power(n, p - 2, p); }
long long fac[1005];
void facinit(int n) {
  fac[0] = 1;
  for (int i = 1; i <= n; i++) fac[i] = fac[i - 1] * i % 1000000007;
}
int nCrModPFermat(int n, int r, long long p) {
  if (r == 0) return 1;
  return (fac[n] * modInverse(fac[r], p) % p * modInverse(fac[n - r], p) % p) %
         p;
}
int main() {
  std::ios::sync_with_stdio(false);
  cin.tie(NULL);
  int erer = 1;
  for (int erer2 = 1; erer2 < erer + 1; erer2++) {
    int n;
    cin >> n;
    int k;
    cin >> k;
    int a[5][1005], pos[5][1005];
    for (int i = 0; i < k; i++)
      for (int j = 0; j < n; j++) {
        cin >> a[i][j];
        pos[i][a[i][j]] = j;
      }
    int dp[1005];
    dp[0] = 1;
    for (int i = 1; i < n; i++) {
      dp[i] = 1;
      for (int j = 0; j < i; j++) {
        int c = 0;
        for (int t = 0; t < k; t++) {
          if (pos[t][a[0][j]] < pos[t][a[0][i]]) {
            c++;
          }
        }
        if (c == k) {
          dp[i] = max(dp[i], dp[j] + 1);
        }
      }
    }
    int big = -1;
    for (int i = 0; i < n; i++) big = max(big, dp[i]);
    cout << big << endl;
  }
  return 0;
}
