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
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/rope>

using namespace std;
// using namespace __gnu_pbds;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef long double ld;

template <class T>

using VV = vector<vector<T>>;

using VI = vector<int>;
using VVI = vector<vector<int>>;

using VL = vector<long long>;
using VVL = vector<vector<long long>>;

using VC = vector<char>;
using VVC = vector<vector<char>>;

using VB = vector<bool>;
using VVB = vector<vector<bool>>;

using VD = vector<double>;
using VVD = vector<vector<double>>;

using PII = pair<int, int>;
using PLL = pair<long long, long long>;
using PIL = pair<int, long long>;
using PLI = pair<long long, int>;

using VPII = vector<pair<int, int>>;
using VPLL = vector<pair<long long, long long>>;

#define LINE '\n'
#define SPACE ' '
#define PB push_back
#define FOR(i, a, b) for (int i = (a); i < (int(b)); i++)
#define FORE(i, a, b) for (int i = (a); i <= (int)((b)); i++)
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
#define sq(a) ((a) * (a))
#define sz(x) ((int)x.size())

#define fastio()                      \
    ios_base::sync_with_stdio(false); \
    cin.tie(nullptr);                 \
    cout.tie(nullptr)
#define debug(x) cerr << #x << " = " << x << endl;

const ll MOD = 1e9 + 7;

namespace debug_internal
{

    void print_value(const string &x) { cerr << '"' << x << '"'; }
    void print_value(const char *x) { cerr << '"' << x << '"'; }
    void print_value(char x) { cerr << '\'' << x << '\''; }
    void print_value(bool x) { cerr << (x ? "true" : "false"); }

    void print_value(short x) { cerr << x; }
    void print_value(unsigned short x) { cerr << x; }
    void print_value(int x) { cerr << x; }
    void print_value(unsigned x) { cerr << x; }
    void print_value(long x) { cerr << x; }
    void print_value(unsigned long x) { cerr << x; }
    void print_value(long long x) { cerr << x; }
    void print_value(unsigned long long x) { cerr << x; }
    void print_value(float x) { cerr << x; }
    void print_value(double x) { cerr << x; }
    void print_value(long double x) { cerr << x; }

#if defined(__SIZEOF_INT128__)
    void print_value(__int128 x)
    {
        if (x == 0)
        {
            cerr << 0;
            return;
        }
        if (x < 0)
        {
            cerr << '-';
            x = -x;
        }
        string s;
        while (x)
        {
            s.push_back(char('0' + x % 10));
            x /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
    void print_value(unsigned __int128 x)
    {
        if (x == 0)
        {
            cerr << 0;
            return;
        }
        string s;
        while (x)
        {
            s.push_back(char('0' + x % 10));
            x /= 10;
        }
        reverse(s.begin(), s.end());
        cerr << s;
    }
#endif

    template <class A, class B>
    void print_value(const pair<A, B> &p);

    template <class... Ts>
    void print_value(const tuple<Ts...> &t);

    template <class T>
    auto print_value(const T &v) -> decltype(begin(v), end(v), void())
    {
        cerr << '{';
        bool first = true;
        for (const auto &x : v)
        {
            if (!first)
                cerr << ", ";
            first = false;
            print_value(x);
        }
        cerr << '}';
    }

    template <class A, class B>
    void print_value(const pair<A, B> &p)
    {
        cerr << '(';
        print_value(p.first);
        cerr << ", ";
        print_value(p.second);
        cerr << ')';
    }

    template <size_t I = 0, class... Ts>
    void print_tuple(const tuple<Ts...> &t)
    {
        if constexpr (I < sizeof...(Ts))
        {
            if constexpr (I)
                cerr << ", ";
            print_value(get<I>(t));
            print_tuple<I + 1>(t);
        }
    }

    template <class... Ts>
    void print_value(const tuple<Ts...> &t)
    {
        cerr << '(';
        print_tuple(t);
        cerr << ')';
    }

    void split_names(const string &s, vector<string> &out)
    {
        string cur;
        int bal = 0;
        for (char c : s)
        {
            if (c == ',' && bal == 0)
            {
                size_t l = 0, r = cur.size();
                while (l < r && isspace((unsigned char)cur[l]))
                    ++l;
                while (r > l && isspace((unsigned char)cur[r - 1]))
                    --r;
                out.push_back(cur.substr(l, r - l));
                cur.clear();
            }
            else
            {
                if (c == '(' || c == '[' || c == '{' || c == '<')
                    ++bal;
                if (c == ')' || c == ']' || c == '}' || c == '>')
                    --bal;
                cur += c;
            }
        }
        size_t l = 0, r = cur.size();
        while (l < r && isspace((unsigned char)cur[l]))
            ++l;
        while (r > l && isspace((unsigned char)cur[r - 1]))
            --r;
        if (l < r)
            out.push_back(cur.substr(l, r - l));
    }

    void print_named(const vector<string> &, int) { cerr << '\n'; }

    template <class Head, class... Tail>
    void print_named(const vector<string> &names, int idx, Head &&H, Tail &&...T)
    {
        if (idx)
            cerr << " | ";
        cerr << names[idx] << " = ";
        print_value(H);
        if constexpr (sizeof...(T))
            print_named(names, idx + 1, forward<Tail>(T)...);
        else
            cerr << '\n';
    }

    template <class T>
    decltype(auto) tee_impl(const char *expr, T &&x, const char *file, int line)
    {
        cerr << '[' << file << ':' << line << "] " << expr << " = ";
        print_value(x);
        cerr << '\n';
        return forward<T>(x);
    }

} // namespace debug_internal

#define DBG(...)                                             \
    do                                                       \
    {                                                        \
        vector<string> _names;                               \
        debug_internal::split_names(#__VA_ARGS__, _names);   \
        cerr << "[L" << __LINE__ << "] ";                    \
        debug_internal::print_named(_names, 0, __VA_ARGS__); \
    } while (0)

#define TEE(x) debug_internal::tee_impl(#x, (x), __FILE__, __LINE__)

template <class T>
inline void maxi(T &a, T b)
{
    a = max(a, b);
}

template <class T>
inline void mini(T &a, T b)
{
    a = min(a, b);
}

template <class T>
inline void addi(T &a, T b)
{
    a = (a + b) % MOD;
}

template <class T>
inline void subi(T &a, T b)
{
    a = (a - b + MOD) % MOD;
}

template <class T>
inline T add(T a, T b)
{
    return (a + b) % MOD;
}

template <class T>
inline T sub(T a, T b)
{
    return (a - b + MOD) % MOD;
}

template <class T>
inline T mul(T a, T b)
{
    return ((a % MOD) * (b % MOD)) % MOD;
}

constexpr ll binpow(ll a, ll b, ll mod)
{
    ll res = 1;
    while (b > 0)
    {
        if (b & 1)
        {
            res = (res * a) % mod;
        }
        a = (a * a) % mod;
        b >>= 1;
    }

    return res;
}

const int INF = 1e9;

const int MAX_N = 1e6 + 3;


void solve()
{
    VVI g(MAX_N);
    VI dp(MAX_N, 0);
    VI par(MAX_N, -1);
    VVI at(MAX_N);

    int k, n1;
    cin >> k >> n1;

    VI pre;
    int id = 1;
    FOR(i, 0, n1)
    {
        at[1].PB(id);
        pre.PB(id++);
        
    }

    
    FOR(i, 2, k + 1)
    {
        int ni;
        cin >> ni;
           VI cur;
        FOR(j, 0, ni)
        {
            int a;
            cin >> a;

            cur.PB(id);
            at[i].PB(id);
            if (a != 0)
            {
                par[id] = pre[a - 1];
            }
            id++;
        }
        pre = cur;
    }


    for(int i = id-1; i >= 1; i--) {
        maxi(dp[i], 1);
        if(par[i] != -1) {
            dp[par[i]] += dp[i];
        }
    }

    int ans = 0;
    FOR(i, 1, k + 1) {
        int d = 0;
        for(auto v : at[i]) d += dp[v];
        maxi(ans, d);
    }
    cout << ans << LINE;
}

int main()
{
    fastio();

    int t = 1;
    // cin >> t;

    while (t--)
        solve();
}