#include <cstdio>
#include <algorithm>
int n, lp[109], rp[109], l[109], r[109];
int gcd(int x, int y) {
	if (y == 0) return x;
	return gcd(y, x % y);
}
int solve(int pos) {
	if (pos == -1) return 1;
	int ls = solve(l[pos]) * lp[pos];
	int rs = solve(r[pos]) * rp[pos];
	int p = ls / gcd(ls, rs) * rs;
	return p / lp[pos] + p / rp[pos];
}
int main() {
	while (scanf("%d", &n), n) {
		for (int i = 0; i < n; i++) scanf("%d%d%d%d", &lp[i], &rp[i], &l[i], &r[i]), l[i]--, r[i]--;
		int ret = 0;
		for (int i = 0; i < n; i++) ret = std::max(ret, solve(i));
		printf("%d\n", ret);
	}
	return 0;
}