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
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>
#include <vector>

using namespace std;

long long gcd(long long int a, long long int b) {
    if (b == 0)
        return a;
    return gcd(b, a % b);
}

long long lcm(long long a, long long b) {
    if (a > b)
        return (a / gcd(a, b)) * b;
    else
        return (b / gcd(a, b)) * a;
}

int calculate_offset(int n, vector<vector<vector<int>>>& masters, int node) {
    int offset_sum = 0;

    for (int i = 0; i < masters[node].size(); i++) {
        int l_m = masters[node][i][0]; // l_m
        int p_s = masters[node][i][1]; // p_s
        int offset = (n - p_s + l_m - 1) / l_m;
        offset_sum += offset;
    }

    return offset_sum;
}

long long combined_reset_time(int master_node, int l_m, int l_s, int p_s, vector<vector<int>>& outputs,
    vector<vector<vector<int>>>& masters) {
    int k = l_m / l_s + 1;
    int surplus = k * l_s - l_m;
    int min_reset_time = -1;
    
    int s_o = 0;

    //int first = surplus * l_m - /*s_o(n)*/ + p_s - l_m + 1; 
    //int first_mod = first % l_m;
    //int reset_time = first + (l_m - (first % l_m) % l_m);

    // add binary search

    int a, b;

    for (int i = 1; i <= 100; i++) {
        int o = calculate_offset(i, masters, master_node);
        int p_s_o = (p_s - o + l_m) % l_m;
        int o_s = (i - p_s_o + l_m - 1) / l_m;
        int L = o_s - o;
        int R = surplus;

        if ((i - o + l_m) % l_m == 0 && (i - o_s + l_s) % l_s == 0) {
            min_reset_time = i;
            break;
        }
    }

    return min_reset_time * 2;
}

void print_vector(vector<int>& v) {
    for (auto i : v) {
        cout << i << " ";
    }
    cout << endl;
}

int main()
{
    int n;
    cin >> n;
    vector<vector<int>> outputs(n + 1);
    vector<int> counters(n + 1);
    int outputs_number, output_to;

    for (int i = 1; i <= n; i++) {
        cin >> outputs_number;

        for (int j = 0; j < outputs_number; j++) {
            cin >> output_to;
            outputs[i].push_back(output_to);
        }
    }

    if (outputs[1].size() == 0) {
        cout << 1;
        return 0;
    }

    vector<vector<vector<int>>> masters(n + 1);

    unsigned long long counter = 0;
    unsigned long long suits = 0;

    int currentNode, nextNode;
    int increment;
    int newCounter;

    do {
        currentNode = 1;
        
        while (outputs[currentNode].size() > 0) {
            nextNode = outputs[currentNode][counters[currentNode]];
            
            newCounter = (counters[currentNode] + 1) % outputs[currentNode].size();

            counter += newCounter - counters[currentNode];
            counters[currentNode] = newCounter;

            currentNode = nextNode;
        }

        suits++;
        //cout << counter << endl;
    } 
    while (counter != 0);

    cout << suits << endl;

    return 0;
}