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

k, n1 = [int(component) for component in input().split(" ")]
days = []
confs = [
  [0] * n1
]
weight = [
  [0] * n1
]
leaf = [
  [True] * n1
]
for i in range(2, k+1):
  arr = list(map(int,sys.stdin.readline().split()))
  arr = arr[1:]
  confs.append(arr)
  weight.append([0] * len(arr))
  leaf.append([True] * len(arr))
# print(confs)

for i in range(k-1, -1, -1):
  for j in range(len(confs[i])):
    weight[i][j] = max(weight[i][j], 1)
    if confs[i][j] == 0: continue
    pred_index = confs[i][j] - 1
    weight[i-1][pred_index] += weight[i][j]
    leaf[i-1][pred_index] = False
    weight[i][j] = 0

max_needed = 0
curr_free = 0

for i in range(k):
  total_weight = sum(weight[i])
  if total_weight > curr_free:
    max_needed += total_weight - curr_free
    curr_free = 0
  # Free the weight 
  curr_free += sum(leaf[i])

print(max_needed)