1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
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();
        }
    }
}