#include<bits/stdc++.h>
#define fi first
#define se second
#define ll long long
using namespace std;
typedef pair<ll, ll> ii;
const ll N = 1e3 + 5;
const ll MOD = 2004010501;
const ll inf = 1e18;
ll n, m, a[N][N], pos[N][N], f[N], ans;
int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0); cout.tie(0);
    cin>>n>>m;

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n;  j++) cin>>a[i][j];
    }

    // Moi so xuat hien dung 1 lan trong 1 day
    // Gia su day con chung dai nhat la c1, c2,..., ck
    // => No cung la day con cua m day
    // => vi tri c1 < vi tri c2 < .. < vi tri ck trong ca m day

    for (int i = 1; i <= m; i++) {
        for (int j = 1; j <= n; j++) pos[i][a[i][j]] = j;
    }

    // f[i] : do dai day con chung dai nhat xet tu ket thuc tai i cua day 1
    for (int i = 1; i <= n; i++) {
        f[i] = 1;
        for (int j = 1; j < i; j++) {
            // f[j] cap nhat cho f[i] neu vi tri a[j] nho hon a[i] o ca m day

            bool ok = true;
            for (int k = 2; k <= m; k++) {
                if (pos[k][a[1][j]] > pos[k][a[1][i]]) {
                    ok = false;
                    break;
                }
            }

            if (ok) f[i] = max(f[i], f[j] + 1);
        }

        ans = max(ans, f[i]);
    }

    cout<<ans;
}
