#include <stdio.h>
#pragma warning(disable : 4996)

typedef struct{
	int x;
	int y;
}DATA;

int main(){
	int n, x, d;
	int i;
	int hmin,hmax,wmin,wmax;
	DATA data[200];

	data[0].x=0;
	data[0].y=0;
	hmin=0;
	hmax=0;
	wmin=0;
	wmax=0;

	scanf("%d", &n);
	while(true){
		if(n!=0){
			for(i=1; i<n; i++){
				scanf("%d%d",&x ,&d);
				if(d == 0){
					data[i].x=data[x].x-1;
					data[i].y=data[x].y;
				}else if(d == 1){
					data[i].y=data[x].y-1;
					data[i].x=data[x].x;
				}else if(d == 2){
					data[i].x=data[x].x+1;
					data[i].y=data[x].y;
				}else if(d == 3){
					data[i].y=data[x].y+1;
					data[i].x=data[x].x;
				}
				if(data[i].x < wmin)wmin=data[i].x;
				if(data[i].x > wmax)wmax=data[i].x;
				if(data[i].y < hmin)hmin=data[i].y;
				if(data[i].y > hmax)hmax=data[i].y;
			}
		}else{
			break;
		}
		printf("%d %d\n",wmax-wmin+1 ,hmax-hmin+1);
		scanf("%d", &n);
	}

	return 0;
}