//03
#include<iostream>

using namespace std;

int p[100],q[100],r[100],b[100];

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

int dfs(int s){
  if(s==0)return 1;
  int x=dfs(r[s])*p[s],y=dfs(b[s])*q[s];
  int z=x/gcd(x,y)*y;
  return z/p[s]+z/q[s];
}

int main(){
  for(int n;cin>>n,n;){
    int fs=n*(n+1)/2;
    for(int i=1;i<=n;i++){
      cin>>p[i]>>q[i]>>r[i]>>b[i];
      fs-=r[i]+b[i];
    }
    cout<<dfs(fs)<<endl;
  }
  return 0;
}