The Lightest Mobile

No attempts yetTime limit1sMemory limit128 MB

Problem

A mobile is a well-known kind of kinetic art. To promote an informatics olympiad, the organizing committee decided to build a mobile. A mobile is assembled from three kinds of parts — rods, strings, and weights — according to the following rules.

  • One end of a rod is painted blue and the other end is painted red.
  • A rod is hung by a string from a single fulcrum located strictly between its two ends.
  • The distance from the fulcrum to the red end and the distance from the fulcrum to the blue end are both positive integers.
  • From each end of a rod, either a weight or another rod is hung by a string.
  • A weight is hung by a string from one end of some rod.
  • Nothing is hung below a weight.
  • The mass of a weight is a positive integer.
  • Exactly one string has one end tied to the fulcrum of a rod (to hang that rod) while its other end is tied to nothing else. Every other string satisfies one of:
    • it connects an end of a rod to the fulcrum of another rod, or
    • it connects an end of a rod to a weight.

Every rod must be balanced. The rods and strings are so light that their masses are treated as $0$. Thus a rod is balanced when

$$(\text{mass hanging below its red end}) \times (\text{fulcrum-to-red length}) = (\text{mass hanging below its blue end}) \times (\text{fulcrum-to-blue length}).$$

A simple mobileNot a mobile, because it is not balanced
A mobileNot a mobile

The structure of the mobile — which rods are used, their length ratios, and how the parts are connected — is already fixed; only the masses of the weights are still undecided. A lighter mobile is easier to hang, so we want it to be as light as possible. Assign a positive-integer mass to every weight so that every rod is balanced and the total mass of the mobile is minimized, then output that minimum total mass. The mobile is described by:

  • the number of rods $n$;
  • for each rod (rods are numbered $1$ to $n$): the ratio of the fulcrum-to-red length to the fulcrum-to-blue length, the number of the rod hung at the red end ($0$ if a weight is hung there), and the number of the rod hung at the blue end ($0$ if a weight is hung there).
The lightest mobile for this structureA mobile that is not the lightest

Input

The first line contains the number of rods $n$ ($1 \le n \le 100$). Each of the next $n$ lines describes one rod: line $i+1$ ($1 \le i \le n$) contains four integers $p$, $q$, $r$, $b$ separated by spaces. For rod $i$, the ratio of the fulcrum-to-red length to the fulcrum-to-blue length is $p : q$, the rod hung at the red end has number $r$, and the rod hung at the blue end has number $b$. A rod number of $0$ means a weight is hung there instead. In every input, if $w$ is the minimum total mass of the mobile and $L$ is the largest positive integer used to express a ratio in the input, then $wL < 2^{31}$.

Output

Output a single line containing the minimum total mass of the mobile.