#include <cstdio>
#include <cstring>

int n;
int bodate[101][5];//右ロン、左ロン、右子、左子、親。
int roots;

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



int chains(int now){
  int mim=0;
  if(bodate[now][2]!=0) mim=chains(bodate[now][2]);
  else mim=1;
  int sam=0;
  if(bodate[now][3]!=0) sam=chains(bodate[now][3]);
  else sam=1;
  int p=0;
  int mihi,sahi;
  p=gcd(bodate[now][0],bodate[now][1]);
  mihi=bodate[now][1]/p;
  sahi=bodate[now][0]/p;
  int mip,sap;
  p=gcd(mim*sahi,sam*mihi);
  mip=sam*mihi/p;
  sap=mim*sahi/p;
  return mim*mip+sam*sap;
}

int main(void){
  scanf("%d",&n);
  while(n!=0){
    int i;
    roots=0;
    memset(bodate,0,sizeof(bodate));
    for(i=1;i<=n;i++){
      scanf("%d%d%d%d",&(bodate[i][0]),&(bodate[i][1]),&(bodate[i][2]),&(bodate[i][3]));
      if(bodate[i][2]!=0)bodate[bodate[i][2]][4]=i;
      if(bodate[i][3]!=0)bodate[bodate[i][3]][4]=i;
    }
    i=1;
    while(bodate[i][4]!=0){
      i=bodate[i][4];
    }
    roots=i;
    int ans=chains(roots);

    printf("%d\n",ans);
    scanf("%d",&n);
  }
  return 0;
}