var i,root,n:longint;
sum:int64;
fa:array[0..1000]of boolean;
p,q,r,b:array[1..1000]of longint;
function gcd(a,b:longint):longint;
begin
if b=0 then gcd:=a
else gcd:=gcd(b,a mod b);
end;
procedure build(k:longint; var ans:int64);
var t,d1,d2:int64;
begin
if r[k]<>0 then build(r[k],d1) else d1:=1;
if b[k]<>0 then build(b[k],d2) else d2:=1;
t:=(p[k]*d1)*(q[k]*d2) div gcd(p[k]*d1,q[k]*d2);
ans:=t div p[k]+ t div q[k];
end;
begin
readln(n);
for i:=1 to n do begin
readln(p[i],q[i],r[i],b[i]);
fa[r[i]]:=true;
fa[b[i]]:=true;
end;
for i:=1 to n do
if fa[i]=false then root:=i;
build(root,sum);
writeln(sum);
close(input);close(output);
end.