#include <bits/stdc++.h>
using namespace std;
int m, n;
struct info {
  int x[6];
};
info List[1005];
bool cmp(info a, info b) {
  int p = 0, q = 0;
  for (int i = 0; i < m; i++) {
    p += a.x[i];
    q += b.x[i];
  }
  return (p < q);
}
bool ok(info a, info b) {
  for (int i = 0; i < m; i++) {
    if (a.x[i] > b.x[i]) return false;
  }
  return true;
}
int cal[1005][1005];
int solve(int now, int last) {
  if (now > n) {
    return 0;
  }
  if (cal[now][last] != -1) {
    return cal[now][last];
  }
  cal[now][last] = 0;
  if (last == 0) {
    return cal[now][last] = max(1 + solve(now + 1, now), solve(now + 1, last));
  } else {
    cal[now][last] = solve(now + 1, last);
    if (ok(List[last], List[now])) {
      cal[now][last] = max(cal[now][last], 1 + solve(now + 1, now));
    }
  }
  return cal[now][last];
}
void print() {
  for (int i = 0; i < 10; i++) {
    for (int j = 0; j < m; j++) {
      cout << List[i].x[j] << " ";
    }
    cout << endl;
  }
}
int main() {
  memset(cal, -1, sizeof(cal));
  cin >> n >> m;
  for (int i = 0; i < m; i++) {
    for (int j = 0; j < n; j++) {
      int p;
      cin >> p;
      assert(List[p].x[i] == 0);
      assert(p > 0 && p <= n);
      List[p].x[i] = j;
    }
  }
  sort(List + 1, List + n + 1, cmp);
  cout << solve(1, 0) << endl;
}
