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
#include <bits/stdc++.h>

using namespace std;
using ll = long long;
#define int ll

const int MAX_N = 100;
vector<int> G[MAX_N];
int state[MAX_N];
int n;
bool reset() {
  for (int i = 0; i < n; i++) {
    if (state[i]) return false;
  }
  return true;
}
void _move(int i = 0) {
  if (G[i].empty()) return;
  int nxt = G[i][state[i]];
  state[i]++;
  state[i] %= G[i].size();
  _move(nxt);
}

int32_t main() {
  cin >> n;
  for (int i = 0; i < n; i++) {
    int r;
    cin >> r;
    for (int j = 0; j < r; j++) {
      int b;
      cin >> b;
      b--;
      G[i].push_back(b);
    }
  }
  _move();
  int ans = 1;
  while (!reset()) {
    _move();
    ans++;
  }
  cout << ans << '\n';
}