### main

loop do
  n = gets.strip.to_i
  break if n == 0

  poles = []
  parents = n.times.map{[]}
  fixed = n.times.map{0}

  for i in (0...n)
    p, q, r, b = gets.strip.split(' ').map{|s| s.to_i}
    g = p.gcd(q)
    r -= 1
    b -= 1
    poles << [p / g, q / g, r, b]

    if r >= 0
      parents[r] << i
    else
      fixed[i] += 1
    end

    if b >= 0
      parents[b] << i
    else
      fixed[i] += 1
    end
  end
  #p poles
  #p parents
  #p fixed

  min_w = nil
  min_ws = []
  queue = (0...n).select{|i| fixed[i] == 2}.to_a

  while ! queue.empty?
    i = queue.shift
    p, q, r, b = poles[i]

    if r < 0
      if b < 0
        min_ws[i] = p + q
      else
        ## p * wr == q * wb (wr = nr * mwr, wb = nb * mwb)
        ## l = lcm(p, q * mwb) ->
        ## wr = l / p, wb = l / q
        ## wr + wq = l/p + l/q
        l = p.lcm(q * min_ws[b])
        min_ws[i] = l / p + l / q
      end
    else
      if b < 0
        l = q.lcm(p * min_ws[r])
        min_ws[i] = l / p + l / q
      else
        l = (p * min_ws[r]).lcm(q * min_ws[b])
        min_ws[i] = l / p + l / q
      end
    end
    #puts "#{i} -> #{min_ws[i]}"

    min_w = min_ws[i]

    for j in parents[i]
      fixed[j] += 1
      if fixed[j] == 2
        queue << j
      end
    end
  end
  #p min_ws

  puts min_w
end