require 'rational'

class AOJ0520
  def initialize(n)
    @n = n
    @poles = []
  end

  def solve
    @n.times{ @poles << gets.split.map(&:to_i) }

    top = ((1..@n).to_a - @poles.map{ |p| [p[2], p[3]] }.flatten)[0]

    weight(top-1)
  end

  def weight(id)
    r = (@poles[id][2] == 0 ? 1 : weight(@poles[id][2]-1))
    l = (@poles[id][3] == 0 ? 1 : weight(@poles[id][3]-1))

    p = @poles[id][0]
    q = @poles[id][1]

    a = (r*q).lcm(l*p) / q
    b = (r*q).lcm(l*p) / p

    a+b
  end
end

while (n = gets.chomp.to_i) != 0
  puts AOJ0520.new(n).solve
end