#include<stdio.h>
typedef struct{
    int x; int y;
}Tile;
//mv[4][x,y]
int mv[4][2]={{-1,0},{0,-1},{1,0},{0,1}};
int main(void){
    while(1){
        Tile tile[200];
        tile[0].x=0,tile[0].y=0;
        int left=0,right=0,up=0,down=0;
        int N;
        scanf("%d",&N);
        if(N==0) break;
        for(int i=1;i<N;i++){
            int n,d;
            scanf("%d%d",&n,&d);
            tile[i].x=tile[n].x+mv[d][0];
            tile[i].y=tile[n].y+mv[d][1];
            if(left>tile[i].x) left=tile[i].x;
            if(right<tile[i].x) right=tile[i].x;
            if(up<tile[i].y) up=tile[i].y;
            if(down>tile[i].y) down=tile[i].y;
        }
        printf("%d %d\n",right-left+1,up-down+1);
    }
}

