#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <climits>
#include <cfloat>
#include <ctime>
#include <map>
#include <utility>
#include <set>
#include <iostream>
#include <memory>
#include <string>
#include <vector>
#include <algorithm>
#include <functional>
#include <sstream>
#include <complex>
#include <stack>
#include <queue>
#include <numeric>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T>
int __builtin_popcount(T n) { return n ? 1 + __builtin_popcount(n & (n - 1)) : 0; }
#endif

#define foreach(it, c) for (__typeof__((c).begin()) it=(c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;


int gcd(int a, int b)
{
	if (a < b)
		swap(a, b);
	if (b == 0)
		return a;
	return gcd(b, a%b);
}
int lcm(int a, int b)
{
	return a/gcd(a, b) * b;
}
int p[101], q[101], r[101], b[101];
int dfs(int s)
{
	if (s == 0)
		return 1;

	int red = dfs(r[s]), blue = dfs(b[s]);
	int t = lcm(red*p[s], blue*q[s]);
	return t/p[s] + t/q[s];
}
int main()
{
	int n;
	while (~scanf("%d", &n) && n)
	{
		bool root[101];
		fill(root, root+101, true);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d%d%d", p+i, q+i, r+i, b+i);
			root[r[i]] = root[b[i]] = false;
		}

		int res = 0;
		for (int i = 1; i <= n; ++i)
			if (root[i])
				res = dfs(i);
		printf("%d\n", res);
	}
}