import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class Sample147 {
	private final Map<Integer, Mobil> map_ = new HashMap<Integer, Mobil>();

	public void add(int[] mobil) {
		Mobil m = new Mobil(map_.size() + 1, mobil[0], mobil[1], mobil[2], mobil[3]);
		map_.put(m.id, m);
	}

	public int calc() {
		int result = 0;
		Map<Integer, Integer> weightMap = new HashMap<Integer, Integer>();
		for (Mobil mobil: map_.values()) {
			int weight = calcWeight(mobil, weightMap);
			if (result < weight) {
				result = weight;
			}
		}
		return result;
	}
	private int calcWeight(Mobil mobil, Map<Integer, Integer> weightMap) {
		Integer value = weightMap.get(mobil.id);
		if (value != null) return value.intValue();

		int weight = 0;
		int g = gcd(mobil.redLength, mobil.blueLength);
		if (mobil.redId == 0 && mobil.blueId == 0) {
			weight = mobil.redLength / g + mobil.blueLength / g;
		} else if (mobil.redId == 0) {
			int blueWeight = calcWeight(map_.get(mobil.blueId), weightMap);
			int redWeight = mobil.blueLength * blueWeight / g;
			weight = redWeight + blueWeight * mobil.redLength / g;
		} else if (mobil.blueId == 0) {
			int redWeight = calcWeight(map_.get(mobil.redId), weightMap);
			int blueWeight = mobil.redLength * redWeight / g;
			weight = blueWeight + redWeight * mobil.blueLength / g;
		} else {
			int blueWeight = calcWeight(map_.get(mobil.blueId), weightMap);
			int redWeight = calcWeight(map_.get(mobil.redId), weightMap);
			int gcd = gcd(redWeight * mobil.redLength, blueWeight * mobil.blueLength);
			weight = blueWeight * redWeight * mobil.redLength / gcd
				+ redWeight * blueWeight * mobil.blueLength/ gcd;
		}
		weightMap.put(mobil.id, weight);
		return weight;
	}
	private int gcd(int n, int m) {
		if (m > n) return gcd(m, n);
		int r = n % m;
		if (r == 0) return m;
		return gcd(m, r);
	}


	public static class Mobil {
		public final int id;
		public final int redLength;
		public final int blueLength;
		public final int redId;
		public final int blueId;

		public Mobil(int id, int redLen, int blueLen, int redId, int blueId) {
			this.id = id;
			this.redLength = redLen;
			this.blueLength = blueLen;
			this.redId = redId;
			this.blueId = blueId;
		}
	}


	private static int[] toIntArray(String[] input) {
		int[] result = new int[input.length];
		int index = 0;
		for (String s : input) {
			result[index++] = Integer.parseInt(s);
		}
		return result;
	}

    public static void main(String[] args) {
		BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
		try {
			final int n = Integer.parseInt(reader.readLine());
			Sample147 sample = new Sample147();
			for (int index = 0; index < n; index++) {
				sample.add(toIntArray(reader.readLine().split("\\s+")));
			}
			int result = sample.calc();
			System.out.println(result);
		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}
}
