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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <bits/stdc++.h>
using namespace std;

const int N = 105;

int k[N];
int kraw[N][N];

typedef long long ll;

string wyn[N];

int mnozenie[50];

int n;

ll nwd(ll x, ll y){
    if(y == 0) return x;
    return nwd(y, x % y);
}

ll reszta(string a, ll b){
    ll re = 0;

    for(int i = 0; i < a.length(); i++){
        re = (re * 10 + (a[i] - '0')) % b;
    }

    return re;
}

ll nwd_string(string a, ll b){
    return nwd(b, reszta(a, b));
}

string pomnoz(string a, string b){
    if(a.length() < b.length()) swap(a, b);

    string product(a.length() + b.length(), 0);
    for (int i = a.length() - 1; i >= 0; i--) {
        for (int j = b.length() - 1; j >= 0; j--) {
            int n = (a[i] - '0') * (b[j] - '0') + product[i + j + 1];
            product[i + j + 1] = n % 10;
            product[i + j] += n / 10;
        }
    }
    for (int i = 0; i < product.size(); i++) {
        product[i] += '0';
    }
    if (product[0] == '0') {
        return product.substr(1);
    }
    return product;
}

string podziel(string a, int b) {

    string result;
    int index = 0;
    

    int c = a[index] - '0';
    while (c < b) {
        c = c * 10 + (a[++index] - '0');
    }
    
    while (a.length() > index) {
        result += (c / b) + '0';
        c = (c % b) * 10 + a[++index] - '0';
    }
    if (result.length() == 0) {
        return "0";
    }
    return result;
}

string dodaj(string a, string b){
    string c = "";

    int al = a.length(), bl = b.length();

    if(al < bl) {
        swap(a, b);
        swap(al, bl);
    }

    int dodaj = 0;

    for(int i = bl - 1; i >= 0; i--){
        int x = b[i] - '0' + a[i + al - bl] - '0' + dodaj;
        dodaj = x / 10;
        x %= 10;
        c += char(x + '0');
    }

    for(int i = al - bl - 1; i >= 0; i--){
        int x = a[i] - '0' + dodaj;
        dodaj = x / 10;
        x %= 10;
        c += char(x + '0');
    }

    if(dodaj > 0) c += char(dodaj + '0');
    string d = "";
    for(int i = c.length() - 1; i >= 0; i--) d += c[i];
    //cout << c << "\n";
    //cout << a << " " << b << " " << d  << endl;
    return d;
}

void calc(){
    for(int i = 2; i <= n; i++) wyn[i] = "0";

    for(int i = 1; i <= n; i++){
        if(k[i] == 0) continue;

        if(reszta(wyn[i], k[i]) > 0){
            ll x = nwd_string(wyn[i], k[i]);
            ll y = k[i] / x;
            //cout << "x " << x << "\n";
            wyn[1] = pomnoz(wyn[1], to_string(y));
            calc();
            return;
        }

        string plus = podziel(wyn[i], k[i]);

        for(int j = 1; j <= k[i]; j++){
            wyn[kraw[i][j]] = dodaj(wyn[kraw[i][j]], plus);
            //cout << dodaj(wyn[kraw[i][j]], plus) << "xxx\n";
        }
    }
}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    cin >> n;

    for(int i = 1; i <= n; i++){
        cin >> k[i];

        for(int j = 1; j <= k[i]; j++)
            cin >> kraw[i][j];
    }

    wyn[1] = "1";

    calc();

    cout << wyn[1] << "\n";
    //for(int i = 1; i <= n; i++) cout << wyn[i] << "\n";

    //cout << dodaj("16", "0") << "\n";
    //cout << dodaj("0", "16") << "\n";
}