class Bar
  def initialize p, q, r, b
    @p = p
    @q = q
    @r = r
    @b = b
    @ref_count = 0
    @left = nil
    @right = nil
  end

  def w
    l = @left.w * @p
    r = @right.w * @q
    lcm = (l * r) / euclid(l, r)
    lcm / @p + lcm / @q
  end

  attr_accessor :left, :right, :ref_count
  attr_reader :p, :q, :r, :b
end

class Weight
  def initialize
    @w = 1
  end
  attr_reader :w
end

def main
  loop do
    n = gets.chomp.to_i
    break if n == 0
    bars = Array.new(n+1, nil)
    (1..n).each do |i|
      p,q,r,b = gets.chomp.split.map(&:to_i)
      bars[i] = Bar.new(p, q, r, b)
    end

    weight = Weight.new
    (1..n).each do |i|
      if bars[i].r == 0
        bars[i].left = weight
      else
        bars[i].left = bars[bars[i].r]
        bars[i].left.ref_count += 1
      end

      if bars[i].b == 0
        bars[i].right = weight
      else
        bars[i].right = bars[bars[i].b]
        bars[i].right.ref_count += 1
      end
    end

    root = nil
    (1..n).each do |i|
      if bars[i].ref_count == 0
        root = bars[i]
        break
      end
    end

    puts root.w
  end
end

def euclid a, b
  m = (a > b ? a : b)
  n = (a <= b ? a : b)
  loop do
    r = m % n
    m = n
    n = r
    break if n == 0
  end
  m
end

main