#include"stdio.h"
#include"string.h"

int table[400][400];//??????????????¨???????????§?¨?(1?????¨????????£?????¢????????¨???????????????table[y??§?¨?][x??§?¨?])
int N;              //??\???????????°
int plate[200][2];  //????????§??????????????£?????¢?????§?¨?
int input[2];       //??\?????????
int i,j;            //?????????
int small,big;      //??£?????¢??????????????£???
int ansLR,ansUD;    //??????

int max(int a,int b){
	if(a>b)return a;
	else   return b;
}
int min(int a,int b){
	if(a<b)return a;
	else   return b;
}

int main(void){
	while(scanf("%d",&N)!=EOF){
		if(N==0)break;
		//?????????
		memset(table,0,400*400*sizeof(int));
		memset(plate,0,200*2*sizeof(int));
		table[200][200]=1;//?????????1???????????????????????????
		plate[0][0]=200;
		plate[0][1]=200;
		small=-1;
		big=-1;
		ansLR=-1;
		ansUD=-1;
		
		for(i=1;i<N;i++){
			//??\???
			scanf("%d %d",&input[0],&input[1]);
			//????????????????????????
			switch(input[1]){
			case 0:
			case 2:
				//??£?????¢??????????????´????????????????????§???????????´???????¨????
				plate[i][0]=plate[input[0]][0];
				plate[i][1]=plate[input[0]][1]+(input[1]-1);
				break;
			case 1:
			case 3:
				plate[i][0]=plate[input[0]][0]-(input[1]-2);
				plate[i][1]=plate[input[0]][1];
				break;
			}
			//??????????????¨???table????¨????
			table[plate[i][0]][plate[i][1]]=1;
		}
		small=200;
		big=200;
		//??¨??¨????????£????????§?????????????¢?????????????
		//?¨?????¢????
		for(i=0;i<400;i++){
			for(j=0;j<400;j++){
				if(table[i][j]==1){
					//small?????£?????¢??????????????????????????????big??????
					small=min(small,j);
					big=max(big,j);
				}
			}
			//??????????????¨??????
			if(small==-1 && big ==-1)continue;
			else if(big==-1)ansLR=max(ansLR,1);
			else ansLR=max(ansLR,big-small+1);
		}
		small=200;
		big=200;
		//???????¢????
		for(j=0;j<400;j++){
			for(i=0;i<400;i++){
				if(table[i][j]==1){
					//small?????£?????¢??????????????????????????????big??????
					small=min(small,i);
					big=max(big,i);
				}
			}
			//??????????????¨??????
			if(small==-1 && big ==-1)continue;
			else if(big==-1)ansUD=max(ansUD,1);
			else ansUD=max(ansUD,big-small+1);
		}
		printf("%d %d\n",ansLR,ansUD);
		/*
		for(i=190;i<210;i++){
			for(j=190;j<220;j++){
				printf("%d ",table[i][j]);
				//if(i==200 && j==200)printf(".");
			}
			printf("\n");
		}
		*/
	}
	return 0;
}