#include <stdio.h>
#define SIZE 1000
#define INF 999999

int N;
int max(int a[]){
    int i,m=0;
    for(i=0;i<N;i++)
        if(m<a[i]) m=a[i];
    return m;
}
int min(int a[]){
    int i,m=INF;
    for(i=0;i<N;i++)
        if(m>a[i]) m=a[i];
    return m;
}

int main(void){
    int i,p,q;
    int x[SIZE],y[SIZE]; //x[i],y[i]??????????????????i???????????£?????¢?????§?¨?
    int dx[] = {-1, 0, 1, 0}; //x???????§????
    int dy[] = {0, 1, 0, -1}; //y???????§????
    for(;;){
        scanf("%d", &N);
        if(N==0) break;
        x[0] = y[0] = 0; //???????????????(0,0)
        for(i=1;i<N;i++){
            scanf("%d %d", &p,&q); //p?????£?????¢???q???????????????
            x[i]=x[p]+dx[q];
            y[i]=y[p]+dy[q];
        }
        //??\?????????????????????x,y????????????????????§?????¨????°????????????¢????????§???????????????
        printf("%d %d\n", max(x)-min(x)+1, max(y)-min(y)+1);
    }
    return 0;
}