import Control.Monad (replicateM)
import Data.Array (listArray,(!))

type StickInfo = (Int,Int,Int,Int)
data Mobile = Stick Integer Integer Mobile Mobile
            | Weight

main = do
    n <- readLn
    if n == 0 then return () else do
        ss <- replicateM n (getLine >>= return . stickInfo . map read . words)
        print $ minWeight $ mobile n ss
        main

    where
    stickInfo [a,b,c,d] = (a,b,c,d)

mobile :: Int -> [StickInfo] -> Mobile
mobile n ss = mobile' root
    where
    root = head $ filter (not . (`elem` sub)) [1..n]
    sub = (map (\(_,_,a,_)->a) ss) ++ (map (\(_,_,_,b)->b) ss)
    sa = listArray (1,n) ss

    mobile' 0 = Weight
    mobile' n = Stick (fromIntegral a) (fromIntegral b) (mobile' c) (mobile' d)
        where
        (a,b,c,d) = sa ! n

minWeight :: Mobile -> Integer
minWeight Weight = 1
minWeight (Stick l1 l2 m1 m2) = lcm' `seq` (lcm' `div` l1) + (lcm' `div` l2)
    where
    lcm' = lcm (l1 * w1) (l2 * w2)
    w1 = minWeight m1
    w2 = minWeight m2