#include <stdio.h>

#define swap(a,b) do {\
    a ^= b;\
    b ^= a;\
    a ^= b;\
} while(0)

typedef struct _Mobile {
    struct _Mobile *parent;
    struct _Mobile *left_vertex;
    struct _Mobile *right_vertex;
    int weight;
    int left_length;
    int right_length;
} Mobile;


int gcd(int a, int b) {
    int c;

    if (a < b) swap(a,b);

    c = a % b;
    while (c) {
        a = b;
        b = c;
        c = a % b;
    }

    return b;
}

void dive(Mobile *vertex) {
    int left_weight;
    int right_weight;
    int gcm;
    int new_left_weight;
    int new_right_weight;

    if (vertex->left_vertex) {
        dive(vertex->left_vertex);
        left_weight = vertex->left_vertex->weight;
    } else {
        left_weight = 0;
    }

    if (vertex->right_vertex) {
        dive(vertex->right_vertex);
        right_weight = vertex->right_vertex->weight;
    } else {
        right_weight = 0;
    }

    gcm = gcd(vertex->left_length, vertex->right_length);
    new_left_weight = vertex->right_length/gcm;
    new_right_weight = vertex->left_length/gcm;

    if (right_weight) {
            int factor = right_weight/gcd(right_weight, new_right_weight);
            new_right_weight *= factor;
            new_left_weight *= factor;
    } 

    if (left_weight) {
            int factor = left_weight/gcd(left_weight, new_left_weight);
            new_left_weight *= factor;
            new_right_weight *= factor;
    }

    vertex->weight = new_left_weight + new_right_weight;
    return;
}

int main() {
    while (1) {
        int i;
        int n;
        Mobile *most_parent;

        scanf("%d", &n);
        if (!n) return 0;

        Mobile vertexes[100];

        for (i=0;i<n;i++) {
            vertexes[i].parent = NULL;
            vertexes[i].weight = 0;
        }

        for (i=0;i<n;i++) {
            int p, q, r, b;
            scanf("%d %d %d %d", &p, &q, &r, &b);
            vertexes[i].left_length = p;
            vertexes[i].right_length = q;
            vertexes[i].left_vertex = (Mobile*)(r==0)?NULL:&vertexes[r-1];
            vertexes[i].right_vertex = (Mobile*)(b==0)?NULL:&vertexes[b-1];

            if (r) {
                vertexes[r-1].parent = &vertexes[i];
            }

            if (b) {
                vertexes[b-1].parent = &vertexes[i];
            }

        }

        Mobile *tmp = &vertexes[0];
        while (tmp->parent) {
            tmp = tmp->parent;
        }
        most_parent = tmp;

        dive(most_parent);

        printf("%d\n", most_parent->weight);
    }
}