#include<stdio.h>

int main(void){
    int N=0;
    int x[201]={0};
    int y[201]={0};
    int maxup=0,maxdown=0,maxright=0,maxleft=0;
    int n=0,d=0;
    int answerx[10000];
    int answery[10000];
    int count=0;
    while(1){
        maxup=0,maxdown=0,maxright=0,maxleft=0;
        scanf("%d",&N);
        if(N==0){
            break;
        }
        for(int i=1;i<N;i++){
            scanf("%d %d",&n,&d);
            if(d==0){
                x[i]=x[n]-1;
                y[i]=y[n];
            }
            else if(d==1){
                x[i]=x[n];
                y[i]=y[n]-1;
            }
            else if(d==2){
                x[i]=x[n]+1;
                y[i]=y[n];
            }
            else if(d==3){
                x[i]=x[n];
                y[i]=y[n]+1;
            }
        }
        for(int i=0;i<N;i++){
            if(x[i]>maxright){
                maxright=x[i];
            }
            if(x[i]<maxleft){
                maxleft=x[i];
            }
            if(y[i]>maxup){
                maxup=y[i];
            }
            if(y[i]<maxdown){
                maxdown=y[i];
            }
        }
        maxdown*=-1;
        maxleft*=-1;
        answerx[count]=maxleft+maxright;
        answery[count]=maxup+maxdown;
        count++;
    }
    for(int i=0;i<count;i++){
        printf("%d %d\n",answerx[i]+1,answery[i]+1);
    }
    return 0;
}
