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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include<bits/stdc++.h>
using namespace std;

#define int int64_t

const int64_t mod = 1e9+7;
const int maxn = 1e6+10;
int inf;


/*
*/

int k, n1;
vector<vector<int>> conf_conts;
vector<vector<int>> conf_cnt;

void tc()
{
    cin >> k >> n1;

    conf_conts.push_back(vector<int>());
    conf_cnt.push_back(vector<int>());
    for (int j = 0; j <= n1; j++)
    {
        conf_conts[0].push_back(0);
        conf_cnt[0].push_back(0);
    }

    //cout << "loading" << endl;
    for (int i = 1; i < k; i++)
    {
        conf_conts.push_back(vector<int>());
        conf_cnt.push_back(vector<int>());

        conf_conts[i].push_back(0);
        conf_cnt[i].push_back(0);

        int len; cin >> len;
        for (int j = 1; j <= len; j++)
        {
            int cont; cin >> cont;
            conf_conts[i].push_back(cont);
            conf_cnt[i].push_back(0);
        }
    }

    //cout << "calc ans" << endl;
    int needed = 0;
    for (int i = k-1; i >= 0; i--)
    {
        
        int needed_today = 0;

        for (int j = 1; j < conf_conts[i].size(); j++)
        {
            if (conf_cnt[i][j] == 0)
            {
                conf_cnt[i][j]++;
                needed_today++;
            }
            else
            {
                needed_today += conf_cnt[i][j];
            }
            //cout << "i=" << i << " j=" << j << " conf_cnt=" << conf_cnt[i][j] << endl;

            int cont_of = conf_conts[i][j];
            //cout << "cont_of=" << cont_of << endl;
            if (cont_of != 0)
            {
                conf_cnt[i-1][cont_of] += conf_cnt[i][j];
            }
        }

        needed = max(needed, needed_today);
    }

    cout << needed << endl;
}

int32_t main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(0);
	inf = numeric_limits<int>::max()/2;

    int T = 1;
    //cin >> T;
    while (T--) tc();

    return 0;
}