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
#include <stdio.h>
#include <string.h>
#define LIMIT 100
unsigned char po[LIMIT][LIMIT];
unsigned char pon[LIMIT];
unsigned char pol[LIMIT] = {0};
char good[LIMIT];

int main() {
  
  int n;
  scanf("%d", &n);
  memset(good, 1, n);
  for(int i=0;i<n;i++) {
    scanf(" %d", pon+i);
    for(int j=0;j<pon[i];j++) {
      int tmp;
      scanf(" %d", &tmp);
      po[i][j] = tmp-1;
    }
  }
  int ans = 0;
  int ngood = n-1;
  good[0] = 0;
  while(ngood < n) {
    ans++;
    int k = 0;
    if(!pon[0]) ngood++;
    while(pon[k]) {
      int tmp = po[k][pol[k]];
      pol[k]++;
      if(pol[k] >= pon[k]) {
        pol[k] = 0;
        if(!good[k]) {
          ngood++;
          good[k] = 1;
        }
      } else {
        if(good[k]) {
          ngood--;
          good[k] = 0;
        }
      } 
      k = tmp;
    }
  }
  printf("%d\n", ans);
  return 0;
}