#include <iostream>
#include <cstring>
using namespace std;

int n,p[101],q[101],r[101],b[101];
bool flg[101];

int gcd(int a,int b){
	return b == 0 ? a : gcd(b,a%b);
}

int lcm(int a,int b){
	return a / gcd(a,b) * b;
}

int solve(int idx){
	int res = 0;

	int rcost = r[idx] ? solve(r[idx]) : 1;
	int bcost = b[idx] ? solve(b[idx]) : 1;
	int l = lcm(p[idx] * rcost, q[idx] * bcost);

	return l / p[idx] + l / q[idx];
}

int main(void){
	while(cin>>n, n){
		memset(flg,0,sizeof(flg));
		for(int i=1;i<=n;i++){
			cin>>p[i]>>q[i]>>r[i]>>b[i];
			flg[r[i]] = flg[b[i]] = true; //ã©çÂé³êÄ¢é_ðtrueÉ·é
		}

		for(int i=1;i<=n;i++){
			//ã©çÂé³êÄ¢È¢_ÈçÎ, [gm[h
			if(!flg[i]){
				cout<<solve(i)<<endl;
				break;
			}
		}
	}

	return 0;
}