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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
#include <bits/stdc++.h>
#define turbo cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);

using namespace std;

const int base = 1000000000;
const int base_num = 9;

struct bignum {
    vector <int> num;
    int sign;

    int size() {
        if (num.empty())
            return 0;
        int res = (num.size() - 1) * base_num;
        int last = num.back();
        while(last) {
            res++; last /= 10;
        }
        return res;
    }

    string to_string() const {
        string res = "";
        if((*this).isZero())
            return "0";
        for (int i = num.size() - 1; i >= 0; i--) {
            string k = std::to_string(num[i]);
            if (i != num.size() - 1)
                while (k.size() < base_num)
                    k = "0" + k;
            res += k;
        }
        return res;
    }

    bignum() : sign(1) {}

    bignum (long long n) {
        *this = n;
    }

    void operator=(const bignum &v) {
		sign = v.sign;
		num = v.num;
	}

    void operator=(long long v) {
        sign = 1;
        num.clear();
        if (v < 0)
			sign = -1, v = -v;
		for (; v > 0; v = v / base)
			num.push_back(v % base);
    }

    bignum operator+(const bignum &v) const {
        if (sign == v.sign) {
			bignum res = v;
 
			for (int i = 0, carry = 0; i < (int) max(num.size(), v.num.size()) || carry; ++i) {
				if (i == (int) res.num.size())
					res.num.push_back(0);
				res.num[i] += carry + (i < (int) num.size() ? num[i] : 0);
				carry = res.num[i] >= base;
				if (carry)
					res.num[i] -= base;
			}
			return res;
		}
		return *this - (-v);
    }

    bignum operator-(const bignum &v) const {
        if (sign == v.sign) {
			if (abs() >= v.abs()) {
				bignum res = *this;
				for (int i = 0, carry = 0; i < (int) v.num.size() || carry; ++i) {
					res.num[i] -= carry + (i < (int) v.num.size() ? v.num[i] : 0);
					carry = res.num[i] < 0;
					if (carry)
						res.num[i] += base;
				}
				res.trim();
				return res;
			}
			return -(v - *this);
		}
		return *this + (-v);
    }

    bignum operator-() const {
		bignum res = *this;
		res.sign = -sign;
		return res;
	}

    bignum abs() const {
		bignum res = *this;
		res.sign *= res.sign;
		return res;
	}

    bool operator<(const bignum &v) const {
		if (sign != v.sign)
			return sign < v.sign;
		if (num.size() != v.num.size())
			return num.size() * sign < v.num.size() * v.sign;
		for (int i = num.size() - 1; i >= 0; i--)
			if (num[i] != v.num[i])
				return num[i] * sign < v.num[i] * sign;
		return false;
	}

    bool operator>(const bignum &v) const {
		return v < *this;
	}
	bool operator<=(const bignum &v) const {
		return !(v < *this);
	}
	bool operator>=(const bignum &v) const {
		return !(*this < v);
	}
	bool operator==(const bignum &v) const {
		return !(*this < v) && !(v < *this);
	}
	bool operator!=(const bignum &v) const {
		return *this < v || v < *this;
	}

    void operator+=(const bignum &v) {
        *this = *this + v;
    }

    void operator-=(const bignum &v) {
        *this = *this - v;
    }

    bignum operator*(int v) const {
        bignum res = *this;
        res *= v;
        return res;
    }

	bignum operator*(long long v) const {
		bignum res = *this;
		res *= v;
		return res;
	}

    void operator*=(long long v) {
		if (v < 0)
			sign = -sign, v = -v;
		if(v > base){
			*this = *this * (v / base) * base + *this * (v % base);
			return ;
		}
		for (int i = 0, carry = 0; i < (int) num.size() || carry; ++i) {
			if (i == (int) num.size())
				num.push_back(0);
			long long cur = num[i] * (long long) v + carry;
			carry = (int) (cur / base);
			num[i] = (int) (cur % base);
		}
		trim();
	}

    friend pair<bignum, bignum> divmod(const bignum &a1, const bignum &b1) {
		int norm = base / (b1.num.back() + 1);
		bignum a = a1.abs() * norm;
		bignum b = b1.abs() * norm;
		bignum q, r;
		q.num.resize(a.num.size());
 
		for (int i = a.num.size() - 1; i >= 0; i--) {
			r *= base;
			r += a.num[i];
			int s1 = r.num.size() <= b.num.size() ? 0 : r.num[b.num.size()];
			int s2 = r.num.size() <= b.num.size() - 1 ? 0 : r.num[b.num.size() - 1];
			int d = ((long long) base * s1 + s2) / b.num.back();
			r -= b * d;
			while (r < 0)
				r += b, --d;
			q.num[i] = d;
		}
 
		q.sign = a1.sign * b1.sign;
		r.sign = a1.sign;
		q.trim();
		r.trim();
		return make_pair(q, r / norm);
	}

    bignum operator/(const bignum &v) const {
        return divmod(*this, v).first;
    }

    bignum operator%(const bignum &v) const {
        return divmod(*this, v).second;
    }

    void operator/=(int v) {
		if (v < 0)
			sign = -sign, v = -v;
		for (int i = (int) num.size() - 1, rem = 0; i >= 0; --i) {
			long long cur = num[i] + rem * (long long) base;
			num[i] = (int) (cur / v);
			rem = (int) (cur % v);
		}
		trim();
	}

    bignum operator/(int v) const {
		bignum res = *this;
		res /= v;
		return res;
	}

    void trim() {
		while (!num.empty() && !num.back())
			num.pop_back();
		if (num.empty())
			sign = 1;
	}

    bool isZero() const {
        return num.empty() || (num.size() == 1 && !num[0]);
    }

    friend bignum gcd(const bignum &a, const bignum &b) {
		return b.isZero() ? a : gcd(b, a % b);
	}

};

const int N = 105;

vector <int> graph[N];
bignum visited[N];

int n;


void readinput() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        int k, x;
        cin >> k;
        for (int j = 0; j < k; j++) {
            cin >> x;
            graph[i].push_back(x);
        }
    }
}

void reset () {
    for (int i = 1; i <= n; i++)
        visited[i] = 0;
}

void DFS(int v) {
    for (auto &u : graph[v])
        visited[u] += visited[v] / graph[v].size();
}

int mygcd (int a, bignum b) {
    multiset <int> rozklad;
    int d = 2;
    while (a > 1) {
        if (a % d == 0) {
            rozklad.insert(d);
            a /= d;
        }
        else
            d++;
    }
    int res = 1;
    for (auto &i : rozklad) {
        if (b % i == 0) {
            b /= i;
            res *= i;
        }
    }
    return res;
}

bignum solve(bignum x) {
    reset();
    visited[1] = x;
    for (int i = 1; i <= n; i++)
        DFS(i);
    for (int i = 1; i <= n; i++) {
        if (!graph[i].size())
            continue;
        if (!(visited[i] % bignum(graph[i].size())).isZero())
            return solve(x * ((int) graph[i].size() / mygcd(graph[i].size(), visited[i])));
    }
    return x;
}

int main() {
    turbo;
    readinput();
    string res = (solve(bignum(1))).to_string();
    cout << res << "\n";
    return 0;
}