
#include<cstdio>
#include<algorithm>
#include<string>
#include<vector>
#include<iostream>
#include<map>

using namespace std;

#define rep(i,n) for(int i=0;i<int(n);i++)
#define reps(i,n) for(int i=1;i<=int(n);i++)

typedef long long ll;
class T{
public:
  int f;
  int s;
  int r;
  int l;
};

T tree[101];

ll sk(ll a,ll b){
  ll nx,nm;
  nx=a;
  nm=b;
  if(a<b){
    swap(a,b);
  }
  while(1){
    if(a%b==0)break;
    ll temp;
    temp=b;
    b=a%b;
    a=temp;
  }
  return nx/b*nm;
}
ll saiki(ll num){
  ll a=1,b=1;
  if(tree[num].f!=0){
    a=saiki(tree[num].f);
  }
  if(tree[num].s!=0){
    b=saiki(tree[num].s);
  }

  ll nn=sk(tree[num].r*a,tree[num].l*b);
  return nn/tree[num].r + nn/tree[num].l;
}
int main(){
  while(1){
  int n;
  scanf("%d",&n);
  if(n==0)break;
  int get[111];
  reps(i,n){
    get[i]=0;
  }
  reps(i,n){
    int a,b,c,d;
    scanf("%d%d%d%d",&a,&b,&c,&d);
    tree[i].r=a;
    tree[i].l=b;
    tree[i].f=c;
    tree[i].s=d;
    get[c]=get[d]=1;
  }
  int st=-1;
  reps(i,n){
    if(get[i]==0){
      st=i;
    }
  }
  ll ans=saiki(st);
  printf("%lld\n",ans);
  }
}