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.
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 mobile | Not a mobile, because it is not balanced |
![]() | ![]() |
| A mobile | Not 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 lightest mobile for this structure | A mobile that is not the lightest |
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 a single line containing the minimum total mass of the mobile.