#include<stdio.h>

int main()
{
    int i,j,k,N,wmax,wmin,hmax,hmin;
    int d[200]={0},n[200]={0};
    int board[401][401];

    while(scanf("%d",&N),N){

        for(i=0;i<401;i++){
            for(j=0;j<401;j++){
                board[i][j]=-1;
            }
        }
        board[200][200]=0;

        for(i=1;i<N;i++){
            scanf("%d%d",&n[i],&d[i]);
        }

        for(k=1;k<N;k++){
            for(i=0;i<401;i++){
                for(j=0;j<401;j++){
                    if(board[i][j]==n[k]){
                        switch(d[k]){
                            case 0:
                                board[i][j-1]=k;
                                break;
                            case 1:
                                board[i+1][j]=k;
                                break;
                            case 2:
                                board[i][j+1]=k;
                                break;
                            case 3:
                                board[i-1][j]=k;
                                break;
                        }
                    }
                }
            }
        }

        wmax=0;
        wmin=401;
        for(i=0;i<401;i++){
            for(j=0;j<401;j++){
                if(board[i][j]>-1){
                    if(wmin>j) wmin=j;
                    break;
                }
            }
            for(k=400;k>=0;k--){
                if(board[i][k]>-1){
                    if(wmax<k) wmax=k;
                    break;
                }
            }
        }

        hmax=0;
        hmin=401;
        for(i=0;i<401;i++){
            for(j=0;j<401;j++){
                if(board[j][i]>-1){
                    if(hmin>j) hmin=j;
                    break;
                }
            }
            for(k=400;k>=0;k--){
                if(board[k][i]>-1){
                    if(hmax<k) hmax=k;
                    break;
                }
            }
        }
        printf("%d %d\n",wmax-wmin+1,hmax-hmin+1);
    }
    return 0;
}