#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>


using namespace std;
typedef long long ll;

#define INF 2000000000
#define LLINF 4000000000000000000
#define SIZE 100

int mo[5] = {0,1,0,-1,0};

struct STICK{
    int l_l,l_r,s_l,s_r;
};

STICK stick[SIZE+10];

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

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

int dfs(int s_num){
    int v_l,v_r,af_l,af_r,times;
    
    if(stick[s_num].s_l == 0 && stick[s_num].s_r == 0) return stick[s_num].l_l + stick[s_num].l_r;
    
    if(stick[s_num].s_l > 0){
        v_l = dfs(stick[s_num].s_l);
    }else{
        v_l = 1;
    }
    
    if(stick[s_num].s_r > 0){
        v_r = dfs(stick[s_num].s_r);
    }else{
        v_r = 1;
    }
    
    
    af_r=lcm(stick[s_num].l_l,v_r);
    af_l=lcm(stick[s_num].l_r,v_l);
    
    times = lcm(af_r/stick[s_num].l_l,af_l/stick[s_num].l_r);
    
    return (stick[s_num].l_l+stick[s_num].l_r) * times;
}

void calc(int n){
    bool top[SIZE+1]={0};
    
    
    for(int i=1;i<=n;i++){
        scanf("%d%d%d%d",&stick[i].l_l,&stick[i].l_r,&stick[i].s_l,&stick[i].s_r);
        
        int G = gcd(stick[i].l_l,stick[i].l_r);
        
        stick[i].l_l/=G;
        stick[i].l_r/=G;
        
        top[stick[i].s_l] =true;
        top[stick[i].s_r] =true;
    }
    
    for(int i=1;i<=n;i++){
        if(top[i]==false){
            printf("%d\n",dfs(i));
            return;
        }
    }
    
}

int main(){
    int n;
    
    while(1){
        scanf("%d",&n);
        if(n==0) break;
        calc(n);
    }
    
    return 0;
}