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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#include <bits/stdc++.h>

#define boost ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int unsigned long long

using namespace std;

const int LUCKY_NUM = 13;
const int MAX_N = 1e2;

int n;


int child, childrenCount, a, b;
int itsToCycleSubgraph[MAX_N + LUCKY_NUM];
int itsWithNode[MAX_N + LUCKY_NUM];

int nullSafe(int X) {
    return X == 0 ? 1 : X;
}

vector<int> childrenList[MAX_N + LUCKY_NUM];
vector<pair<int, int> > parentList[MAX_N + LUCKY_NUM];


int parentsNWW;

int myNWW(int x, int y) {
    int gcd = __gcd(x, y);

    return x * (y / gcd);
}

void calcParentsNWW(int node) {
    parentsNWW = 1;

    for (auto parent: parentList[node]) {
        int parentId = parent.first;

        parentsNWW = myNWW(parentsNWW, itsToCycleSubgraph[parentId]);
    }
}

int itsFromParent(pair<int, int> parent) {
    int parentId = parent.first;
    int childNum = parent.second;
    childrenCount = childrenList[parentId].size();

    int parentIts = itsWithNode[parentId] * (parentsNWW / itsToCycleSubgraph[parentId]);


    return (parentIts + childrenCount - 1 - childNum) / childrenCount;
}


int itsFromParents(int nodeId) {
    int res = 0;
    for (auto parent: parentList[nodeId]) {
        res += itsFromParent(parent);
    }
    return res;
}


void readInput() {
    boost;
    cin >> n;
    for (int node = 1; node <= n; node++) {
        cin >> childrenCount;
        for (int childNum = 0; childNum < childrenCount; childNum++) {
            cin >> child;
            childrenList[node].push_back(child);
            parentList[child].push_back({node, childNum});

        }
    }
}

int calcResult() {
    int result = 1;
    for (int i = 1; i <= n; i++) {
        int sajz = childrenList[i].size();
        if (sajz == 0) {
            result = myNWW(result, itsToCycleSubgraph[i]);
        }
    }
    return result;
}

void solve() {
    itsToCycleSubgraph[1] = nullSafe(childrenList[1].size());
    itsWithNode[1] = itsToCycleSubgraph[1];

    for (int i = 2; i <= n; i++) {
        calcParentsNWW(i);
        a = itsFromParents(i);
        b = nullSafe(childrenList[i].size());
        if (a == 0) {
            itsToCycleSubgraph[i] = 1;
        } else {
            itsWithNode[i] = a * b / __gcd(a, b);
            itsToCycleSubgraph[i] = nullSafe(parentsNWW * itsWithNode[i] / a);
        }
    }
}

void printResult() {
//    for (int nodeId = 1; nodeId <= n; nodeId++) {
//        cout << itsToCycleSubgraph[nodeId] << " ";
//    }
//    cout << endl;
//    for (int nodeId = 1; nodeId <= n; nodeId++) {
//        cout << itsWithNode[nodeId] << " ";
//    }
//    cout << endl;
    cout << calcResult() << endl;
}

int32_t main() {
    readInput();
    solve();
    printResult();

}

/*
7
3 2 3 5
2 3 6
3 5 6 7
1 6
1 7
0
0

3
0
1 3
0
 */