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
#include <bits/stdc++.h>
using namespace std;
#define REP(i,a,b) for (int i = (a); i <= (b); ++i)
#define REPD(i,a,b) for (int i = (a); i >= (b); --i)
#define FORI(i,n) REP(i,1,n)
#define FOR(i,n) REP(i,0,int(n)-1)
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define vi vector<int>
#define ll long long
#define SZ(x) int((x).size())
#define DBG(v) cerr << #v << " = " << (v) << endl;
#define FOREACH(i,t) for (typeof(t.begin()) i=t.begin(); i!=t.end(); i++)
#define fi first
#define se second

const int DIGITS = 6, BASE = 1000000; // uwaga: liczba_blokow*BASE^2 musi sie miescic w longlongu
#define SIZ int(size())
struct bignum : public vi {
   bignum() {
      pb(0);
   }

   bignum(ll num) {
      if(num < BASE) pb(num);
      else while(num > 0) {
         pb(num % BASE);
         num /= BASE;
      }
   }

   bignum(const string &s) {
      for (int i = 0; i < SZ(s); i += DIGITS) {
         int kon = SZ(s)-i, pocz = max(0,kon-DIGITS);
         pb(atoi(s.substr(pocz,kon-pocz).c_str()));
      }
      cut0();
   }

   string str() const {
      stringstream ss;
      ss << back() << setfill('0');
      for(int i = SIZ-2; i>=0; --i) ss << setw(DIGITS) << at(i);
      return ss.str();
   }

   bignum& operator+= (const bignum& y) {
      int maxsz = max(SIZ,SZ(y));
      bool carry = 0;
      FOR(i,maxsz) {
         int sum = carry + (i < SIZ ? at(i) : 0) + (i < SZ(y) ? y[i] : 0);
         if (sum >= BASE) {
            carry = 1;
            sum -= BASE;
         } else {
            carry = 0;
         }
         if (i < SIZ) at(i) = sum;
         else pb(sum);
      }
      if (carry) pb(1);
      return *this;
   }

   void cut0() {
      while (size()>1 && back()==0) pop_back();
   }

   bignum operator* (const bignum& y) const {
      vector<ll> w(size()+SZ(y)+2, 0);

      FOR(i,SIZ) FOR(j,SZ(y)) w[i+j] += (ll)at(i) * y[j];

      ll carry = 0;
      FOR(i,SZ(w)) {
         w[i] += carry;
         carry = w[i] / BASE;
         w[i] %= BASE;
      }

      bignum res;
      res.resize(SZ(w));
      FOR(i,SZ(w)) res[i]=w[i];
      res.cut0();
      return res;
   }

   bignum& operator -= (const bignum &y) { // dziala tylko dla x>=y!
      FOR(i,SIZ) {
         if (i < SZ(y)) at(i) -= y[i];
         if (at(i) < 0) {
            at(i) += BASE;
            at(i+1) -= 1;
         }
      }
      cut0();
      return *this;
   }

   int compare (const bignum &y) const {
      if (SIZ < SZ(y)) return -1;
      if (SIZ > SZ(y)) return 1;
      for (int i = size()-1; i >= 0; --i) if (at(i) != y[i]) {
         if (at(i) < y[i]) return -1;
         else return 1;
      }
      return 0;
   }

   int operator % (int m) const {
      ll w = back()%m;
      for (int i = size()-2; i >= 0; --i) w = (w*BASE + at(i))%m;
      return w;
   }

   bignum& operator /= (int y) { // dziala tylko dla y < BASE!
      ll dz = 0;
      for (int i = size()-1; i >= 0; --i) {
         ll x = dz*BASE + at(i);
         at(i) = x/y;
         dz = x%y;
      }
      cut0();
      return *this;
   }

   bool operator < (const bignum &y) const { return compare(y)<0; }
   bool operator > (const bignum &y) const { return compare(y)>0; }
   bool operator <= (const bignum &y) const { return compare(y)<=0; }
   bool operator >= (const bignum &y) const { return compare(y)>=0; }
   bool operator == (const bignum &y) const { return compare(y)==0; }
   bool operator != (const bignum &y) const { return compare(y)!=0; }
   bignum operator + (const bignum& y) const { bignum res = *this; res += y; return res; }
   bignum operator - (const bignum &y) const { bignum res = *this; res -= y; return res; } // dziala tylko dla x>=y!
   bignum& operator *= (const bignum &y) { return *this = *this * y; }
   bignum operator / (int y) const { bignum res = *this; res /= y; return res; } // dziala tylko dla y < BASE!
   
   int m2() { return at(0)&1; }
   bignum p2(int k) {
	   bignum res = *this;
	   int f = k/30, r = k%30;
	   FOR(i,f) res *= 1<<f;
	   res *= 1<<r;
	   return res;
   }
};
ostream& operator << (ostream &s, const bignum &y) { return s << y.str(); }

bignum bgcd2(bignum a, bignum b) {
	//cout << "b " << a << " " << b << "\n";
	while (b.m2()==0) b/=2;
	if (a==b) return a;
	if (a<b) swap(a,b);
	return bgcd2(b, (a-b)/2);
}

bignum bgcd(bignum a, bignum b) {
	//cout << a << " " << b << "\n";
	int c=0;
	while (a.m2()==0 && b.m2()==0) {
		c++;
		a/=2; b/=2;
	}
	while (a.m2()==0) a/=2;
	while (b.m2()==0) b/=2;
	//cout << a << " " << b << "\n";
	if (a==b) return a.p2(c);
	if (a<b) swap(a,b);
	//cout << a << " " << b << "\n";
	return bgcd2(b, (a-b)/2).p2(c);
}

bignum w[1010];

int main() {
	int n,k,u;
	scanf("%d", &n);
	bignum ans = 1;
	w[1] = 1;
	FORI(i,n) {
		//k = n-i;
		scanf("%d", &k);
		if (k > 0 && w[i] > 0) {
			//bignum c = lcm(lcm(w[i], k) / w[i], lcm(ans, k) / ans);
			int g1 = stoi(bgcd(w[i], k).str());
			int g2 = k;//stoi(bgcd(ans, k).str());
			int c = lcm(k/g1, k/g2);
			if (c > 1) {
				for (int j = i; j <= n; j++) w[j] *= c;
				ans *= c;
			}
		}
		FOR(j,k) {
			//u = i+j+1;
			scanf("%d", &u);
			w[u] += w[i] / k;
		}
	}
	cout << ans << "\n";
	return 0;
}