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

typedef long long int lli;

using namespace std;

#define MAX3 1010
#define MAX4 10010
#define MAX5 100010
#define MAX6 1000010

template <typename T >
void dbg(T last)
{
  cout << last << "\n";
}

template <typename T, typename ...args>
void dbg(T current, args ...next)
{
  cout << current << ' ';
  dbg(next...);
}
// BigInt from https://codeforces.com/contest/98/submission/143267789


constexpr int digits(int base) noexcept {return base <= 1 ? 0 : 1 + digits(base / 10);}constexpr int BASE = 1000'000'000;constexpr int BASE_DIGITS = digits(BASE);
struct BigInt {vector<int> a;int sign;BigInt():sign(1) {}BigInt(long long v) {*this = v;}BigInt& operator=(long long v) 
{sign = 1;if (v < 0) {sign = -1;v = -v;}a.clear();for (; v > 0; v = v / BASE)a.push_back((int)(v % BASE));return *this;}
BigInt(const string &s){read(s);}void read(const string& s) {sign = 1;a.clear();int pos = 0;while (pos < (int) s.size() && (s[pos] == '-' || s[pos] == '+')) {if (s[pos] == '-')
sign = -sign;++pos;}for (int i = (int)s.size() - 1; i >= pos; i -= BASE_DIGITS) {int x = 0;for (int j = max(pos, i - BASE_DIGITS + 1); j <= i; j++)x = x * 10 + s[j] - '0';a.push_back(x);}
trim();}friend istream& operator>>(istream &stream, BigInt &v) {string s;stream >> s;v.read(s);return stream;}friend ostream& operator<<(ostream &stream, const BigInt &v) {if (v.sign == -1 && !v.isZero())stream << '-';
stream << (v.a.empty() ? 0 : v.a.back());for (int i = (int) v.a.size() - 2; i >= 0; --i)stream << setw(BASE_DIGITS) << setfill('0') << v.a[i];
return stream;}bool operator<(const BigInt &v) const {if (sign != v.sign)return sign < v.sign;if (a.size() != v.a.size())
return a.size() * sign < v.a.size() * v.sign;for (int i = ((int) a.size()) - 1; i >= 0; i--)if (a[i] != v.a[i])return a[i] * sign < v.a[i] * sign;return false;
}bool operator>(const BigInt &v) const {return v < *this;}bool operator<=(const BigInt &v) const {return !(v < *this);}bool operator>=(const BigInt &v) const {return !(*this < v);}
bool operator==(const BigInt &v) const {return sign == v.sign && a == v.a;} bool operator!=(const BigInt &v) const {return !(*this == v);}
BigInt operator-() const {BigInt res = *this;if (isZero())return res;res.sign = -sign;return res;}friend int __compare_abs(const BigInt& x, const BigInt& y) {if (x.a.size() != y.a.size()) {return x.a.size() < y.a.size() ? -1 : 1;}
for (int i = ((int) x.a.size()) - 1; i >= 0; --i) {if (x.a[i] != y.a[i]) {return x.a[i] < y.a[i] ? -1 : 1;}}return 0;}void __internal_add(const BigInt& v) {if (a.size() < v.a.size()) {a.resize(v.a.size(), 0);}for (int i = 0, carry = 0; i < (int) max(a.size(), v.a.size()) || carry; ++i) {
if (i == (int) a.size()) a.push_back(0);a[i] += carry + (i < (int) v.a.size() ? v.a[i] : 0);carry = a[i] >= BASE;
if (carry) a[i] -= BASE;}}void __internal_sub(const BigInt& v) {for (int i = 0, carry = 0; i < (int) v.a.size() || carry; ++i) {a[i] -= carry + (i < (int) v.a.size() ? v.a[i] : 0);
carry = a[i] < 0;if (carry) a[i] += BASE;}this->trim();}BigInt& operator+=(const BigInt& v) {if (sign == v.sign) {__internal_add(v);
} else {if (__compare_abs(*this, v) >= 0) {__internal_sub(v);} else {BigInt vv = v;swap(*this, vv);__internal_sub(vv);}}
return *this;}BigInt& operator-=(const BigInt& v) {if (sign == v.sign) {if (__compare_abs(*this, v) >= 0) {__internal_sub(v);} else {BigInt vv = v;swap(*this, vv);
__internal_sub(vv);this->sign = -this->sign;}} else {__internal_add(v);}return *this;}template< typename L, typename R >typename std::enable_if<std::is_convertible<L, BigInt>::value &&std::is_convertible<R, BigInt>::value,BigInt>::type friend operator-(L&& l, R&& r) 
{BigInt result(std::forward<L>(l));result -= r;return result;}friend pair<BigInt, BigInt> divmod(const BigInt &a1, const BigInt &b1) {assert(b1 > 0);
long long norm = BASE / (b1.a.back() + 1);BigInt x = a1.abs() * norm;BigInt y = b1.abs() * norm;BigInt q = 0, r = 0;q.a.resize(x.a.size());
for (int i = (int)x.a.size() - 1; i >= 0; i--) {r *= BASE;r += x.a[i];long long s1 = r.a.size() <= y.a.size() ? 0 : r.a[y.a.size()];long long s2 = r.a.size() <= (int)y.a.size() - 1 
? 0 : r.a[(int)y.a.size() - 1];long long d = (long long)(((long long) BASE * s1 + s2) / y.a.back());r -= y * d;while (r < 0) {r += y, --d;}q.a[i] = d;}q.sign = a1.sign * b1.sign;r.sign = a1.sign;
q.trim();r.trim();auto res = make_pair(q, r / norm);if (res.second < 0) res.second += b1;return res;}BigInt operator/(const BigInt &v) const {if (v < 0) return divmod(-*this, -v).first;return divmod(*this, v).first;}BigInt& operator/=(const BigInt &v) {*this = *this / v;return *this;}
BigInt operator%(const BigInt &v) const {return divmod(*this, v).second;}BigInt& operator%=(const BigInt &v) {*this = *this % v;return *this;}
BigInt mul_simple(const BigInt &v) const {BigInt res;res.sign = sign * v.sign;res.a.resize(a.size() + v.a.size());
for (int i = 0; i < (int) a.size(); ++i)if (a[i])for (int j = 0, carry = 0; j < (int) v.a.size() || carry; ++j) {long long cur = res.a[i + j] + (long long) a[i] * (j < (int) v.a.size() ? 
	v.a[j] : 0) + carry;carry = (int) (cur / BASE);res.a[i + j] = (int) (cur % BASE);}res.trim();return res;}BigInt operator*(const BigInt &v) const {return mul_simple(v);}BigInt& operator*=(const BigInt &v) {*this = *this * v;
return *this;}BigInt& operator/=(int v) {assert(v > 0);if (llabs(v) >= BASE) {*this /= BigInt(v);return *this;}if (v < 0)
sign = -sign, v = -v;for (int i = (int) a.size() - 1, rem = 0; i >= 0; --i) {long long cur = a[i] + rem * (long long) BASE;a[i] = (int) (cur / v);rem = (int) (cur % v);}trim();
return *this;}BigInt operator/(int v) const {assert(v > 0);if (llabs(v) >= BASE) {return *this / BigInt(v);}BigInt res = *this;res /= v;return res;}BigInt& operator*=(int v) {if (llabs(v) >= BASE) {
*this *= BigInt(v);return *this;}if (v < 0)sign = -sign, v = -v;for (int i = 0, carry = 0; i < (int) a.size() || carry; ++i) {if (i == (int) a.size())a.push_back(0);long long cur = a[i] * 
(long long) v + carry;carry = (int) (cur / BASE);a[i] = (int) (cur % BASE);}trim();return *this;}BigInt operator*(int v) const {if (llabs(v) >= BASE) {return *this * BigInt(v);}BigInt res = *this;res *= v;return res;}long long operator%(long long v) const {
assert(v < BASE);if (v < 0)v = -v;long long m = 0;for (int i = (int)a.size() - 1; i >= 0; --i)m = (int)((a[i] + m * (long long) BASE) % v);
return m * sign;}BigInt& operator%=(long long v) {assert(v < BASE);*this = *this % v;return *this;}friend BigInt __gcd(const BigInt &a, const BigInt &b) {BigInt A=a,B=b,ans=1;for (;!(A.a[0]&1)&&!(B.a[0]&1);A/=2,B/=2,ans*=2);for (;;){if (A.a.size()==0&&A.a[0]==0) 
return B*ans;if (B.a.size()==0&&B.a[0]==0) return A*ans;for (;!(A.a[0]&1);A/=2);for (;!(B.a[0]&1);B/=2);A>B?A-=B:B-=A;}}void trim() {while (!a.empty() && a.back()==0)a.pop_back();if (a.empty())sign = 1;}bool isZero() const {return a.empty() || (a.size()==1 && a[0]==0);}BigInt 
abs() const {if(sign == -1)return -*this;return *this;}template< typename L, typename R >typename std::enable_if<std::is_convertible<L, BigInt>::value &&std::is_convertible<R, BigInt>::value &&std::is_lvalue_reference<R&&>::value,BigInt>::type friend operator+(L&& l, R&& r) {BigInt result(std::forward<L>(l));
result += r;return result;}template< typename L, typename R >typename std::enable_if<std::is_convertible<L, BigInt>::value &&std::is_convertible<R, BigInt>::value &&std::is_rvalue_reference<R&&>::value,BigInt>::type friend operator+(L&& l, R&& r) {BigInt result(std::move(r));result += l;return result;}};

//-----------------------------------------------------------------------------------------------//

vector<int> tab[MAX3];
int cnt[MAX3];
BigInt numerator[MAX3];
BigInt denominator[MAX3];
BigInt answerator[MAX3];



int n;

BigInt zero("0");
BigInt one("1");


void add(int i, int j, BigInt x)
{
  BigInt a = numerator[i];
  BigInt b = denominator[i];
  BigInt c = numerator[j];
  BigInt d = denominator[j];

  d*=x;
  if (b == zero)
  {
    numerator[i] = c;
    denominator[i] = d;
  // dbg(i,  c,"/",d);

    return;
  }
  BigInt a1 = a;
  a1*=d;
  BigInt b1 = b;
  b1*=c;
  BigInt n = a1;
  n+=b1;
  BigInt nd = b;
  nd *= d;
  BigInt g =__gcd(n,nd);
  n /= g;
  nd /= g;
  numerator[i] = n;
  denominator[i] = nd;
  // dbg(a,"/",b, " + ", c,"/",d, " = ", n,"/",nd);
  // dbg(i,  c,"/",d);

}

void work()
{
  numerator[1] = 1;
  denominator[1] = 1;
  for (int i=1;i<=n;i++)
  {
    if (denominator[i] != zero && tab[i].size() !=0)
    {
      int xx = tab[i].size();
      BigInt x(xx);
      BigInt an = denominator[i];
      an*=x;
      BigInt gg =__gcd(an,numerator[i]);
      answerator[i] = an;
      answerator[i] /= gg;
      for (int j=0;j<tab[i].size();j++)
      {
          add(tab[i][j], i, x);
      }
    }
  }

  BigInt ans("1");
  // for (int i=1;i<=n;i++)
  // {
  //   if (denominator[i] != zero)
  //   {
  //     ans = (ans*denominator[i])/(__gcd(ans, denominator[i]));
  //     // dbg(numerator[i], denominator[i]);
  //   }
  // }


  for (int i=1;i<=n;i++)
  {
    if (answerator[i] != zero)
    {
      BigInt gg =__gcd(ans, answerator[i]);
      ans *=answerator[i];
      ans /=gg;
      // dbg(denominator[i]);
    }
  }

  cout << ans << "\n";
}

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

  work();
}

int main()
{
  std::ios::sync_with_stdio(false);

  solve();

  return 0;
}


/*

7
3 2 3 5
2 3 6
3 5 6 7
1 6
1 7
0
0

7
3 2 3 5
2 3 6
3 5 6 7
1 6
1 7
0
0

*/