#include <stdio.h>

int main(void)
{
    int t[4][2] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}};

    for (;;) {
        int n, i;
        int l[200][2];
        int lx, ly, sx, sy;

        scanf("%d", &n);
        if (n == 0)
            break;

        l[0][0] = l[0][1] = 0;
        lx = ly = sx = sy = 0;
        for (i = 1; i < n; ++i) {
            int m, d;

            scanf("%d %d", &m, &d);
            l[i][0] = l[m][0] + t[d][0];
            l[i][1] = l[m][1] + t[d][1];

            if (l[i][0] > lx)
                lx = l[i][0];
            if (l[i][1] > ly)
                ly = l[i][1];
            if (l[i][0] < sx)
                sx = l[i][0];
            if (l[i][1] < sy)
                sy = l[i][1];
        }

        printf("%d %d\n", lx - sx + 1, ly - sy + 1);
    }

    return 0;
}