1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
using namespace std;
#define TF(i, p, q) for(int i = (p); i <= (q); ++i)
#define TR(i, q, p) for(int i = (q); i >= (p); --i)
#define V vector
#define pb push_back
#define eb emplace_back
#define fi first
#define se second
#define th third
#define rz resize
#define all(V) V.begin(), V.end()
#define rall(V) V.rbegin(), V.rend()
#define sz(V) (int)(V.size())
#define cmin(a, b) a = min(a, b)
#define cmax(a, b) a = max(a, b);
typedef pair<int,int> ii;
typedef queue<int> qi;
typedef queue<ii> qii;
typedef long long ll; 
typedef vector<int> vi;
typedef vector<vector<int>>vvi;
typedef vector<vector<bool>>vvb;
typedef vector<bool> vb;
typedef vector<ii> vii;
constexpr int inf = 1e9 + 7;
void solve(){
    vvi tab;
    int k, n; cin >> k >> n;
    tab.rz(k+1);
    tab[1].rz(n+1);
    tab[1][0] = n;
    TF(i, 2, k){
        int ck; cin >> ck;
        tab[i].pb(ck);
        TF(j, 1, ck){
            int a; cin >> a;
            tab[i].pb(a);
        }
    }
    vvi dp(k+1);
    TF(i, 1, k){
        dp[i].rz(tab[i][0]+1, 0);
    }
    TR(i, k, 1){
        TF(j, 1, tab[i][0]){
            if(!dp[i][j]) dp[i][j] = 1;
            if(tab[i][j]){
                dp[i-1][tab[i][j]] += dp[i][j];
            }
        }
    }
    int mx = 0;
    TF(i, 1, k){
        int s = 0;
        TF(j, 1, tab[i][0]){
            s += dp[i][j];
        }
        cmax(mx, s);
    }
    cout << mx;
}
int32_t main(){
    ios_base::sync_with_stdio(0); cin.tie(0);
    solve();
}