import core.stdc.stdio;

long gcd(long a,long b){
	return b?gcd(b,a%b):a;
}

long lcd(long a,long b){
	return a/gcd(a,b)*b;
}

void main(){
	while(1){
	
	int n;
	scanf("%d",&n);
	if(!n)
		break;
	auto p=new long[n],q=new long[n];
	auto r=new int[n],b=new int[n],ic=new int[n];
	foreach(i;0..n){
		scanf("%lld%lld%d%d",&p[i],&q[i],&r[i],&b[i]);
		if(--r[i]>=0)
			ic[r[i]]++;
		if(--b[i]>=0)
			ic[b[i]]++;
	}
	int root;
	foreach(int i,c;ic)
		if(!c)
			root=i;
	long dfs(int i){
		if(i<0)
			return 1;
		long s=dfs(r[i]),t=dfs(b[i]);
		//s*p[i]=t*q[i]
		long l=lcd(s*p[i],t*q[i]);
		s=l/p[i];
		t=l/q[i];
		return s+t;
	}
	printf("%lld\n",dfs(root));
	
	}
}