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
import sys

def solve():
    data = sys.stdin.buffer.read().split()
    idx = 0

    k = int(data[idx]); idx += 1
    n1 = int(data[idx]); idx += 1

    all_n = [n1]
    all_parents = [None]  # dzień 1: brak rodziców

    for _ in range(k - 1):
        ni = int(data[idx]); idx += 1
        parents = list(map(int, data[idx:idx + ni]))
        idx += ni
        all_n.append(ni)
        all_parents.append(parents)

    total_leaves = 0
    available = 0   # wolni pracownicy 
    matched = 0     # liczba oszczędności

    for d in range(k):
        n_d = all_n[d]

        # Świeże spotkania na tym dniu (dzień 2+)
        fresh_today = 0 if d == 0 else all_parents[d].count(0)

        # spotkania bez kontynuacji
        if d + 1 < k:
            has_child = bytearray(n_d + 1)
            for p in all_parents[d + 1]:
                if p > 0:
                    has_child[p] = 1
            leaves_today = n_d - sum(has_child[1:n_d + 1])
        else:
            leaves_today = n_d  # ostatni dzień

        total_leaves += leaves_today

        # dopasowuje świeże spotkania z dostępnymi pracownikami
        m = min(available, fresh_today)
        matched += m
        available = available - m + leaves_today

    print(total_leaves - matched)

solve()