#include <stdio.h>


int main(void)
{
    int N;
    int n, d;
    int x[201], y[201];
    int x_min, x_max, y_min, y_max;
    int i;

    while (1){
        scanf("%d", &N);
        if (N == 0){
            break;
        }
        
        x_min = y_min = 0;
        x_max = y_max = 0;
        x[0] = y[0] = 0;
        for (i = 1; i < N; i++){
            scanf("%d%d", &n, &d);
            
            switch (d){
              case 0:   //左
                x[i] = x[n] - 1;
                y[i] = y[n];
                if (x_min > x[i]){
                    x_min = x[i];
                }
                break;
              
              case 1:   //下
                x[i] = x[n];
                y[i] = y[n] + 1;
                if (y_max < y[i]){
                    y_max = y[i];
                }
                break;
              
              case 2:   //右
                x[i] = x[n] + 1;
                y[i] = y[n];
                if (x_max < x[i]){
                    x_max = x[i];
                }
                break;
              
              case 3:   //上
                x[i] = x[n];
                y[i] = y[n] - 1;
                if (y_min > y[i]){
                    y_min = y[i];
                }
                break;
            }
        }
        
        
        printf("%d %d\n", x_max - x_min + 1, y_max - y_min + 1);
    }
    return (0);
}

