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
#include <cstdio>
#include <vector>

using namespace std;

struct Node {
  int id;
  int i=0;
  vector<Node*> neighbours;

  void print() {
    for (int i=0; i<neighbours.size(); i++) {
      printf("%d->%d\n", this->id, this->neighbours[i]->id);
    }
  }
};

bool is_starting_setup(vector<Node> &tasmy) {
  for (int i=0; i<tasmy.size(); i++) {
    if (tasmy[i].i != 0) {
      return false;
    }
  }

  return true;
}

void pusc_walize(vector<Node> &tasmy) {
  Node* current_node = &tasmy[0];

  while(current_node->neighbours.size() > 0) {
    Node* next_node = current_node->neighbours[current_node->i];
    current_node->i = (current_node->i + 1) % current_node->neighbours.size();
    current_node = next_node;
  }
}

int main() {
  int n;
  int result=0;
  vector<Node> tasmy;

  scanf("%d", &n);
  tasmy.resize(n);

  for (int i=0; i<n; i++) {
    int r;
    tasmy[i].id = i + 1;

    scanf("%d", &r);

    for (int j=0; j<r; j++) {
      int l;

      scanf("%d", &l);
      tasmy[i].neighbours.push_back(&tasmy[l - 1]);
    }
  }

  pusc_walize(tasmy);
  result++;

  while(!is_starting_setup(tasmy)) {
    pusc_walize(tasmy);
    result++;
  }

  printf("%d", result);

  return 0;
}