#include <stdio.h>
#include <memory.h>
struct data{
	int h,w;
};
int main(void){
	struct data box[200];
	int n,t,d,i,minw,maxw,minh,maxh;
	while(1){
		scanf("%d",&n);
		if(!n)break;
		memset(box,0,sizeof(box));
		box[0].h=box[0].w=0;
		for(i=1;i<n;i++){
			scanf("%d %d",&t,&d);
			switch(d){
				case 0:box[i].w=box[t].w-1; box[i].h=box[t].h; break;
				case 1:box[i].w=box[t].w; box[i].h=box[t].h-1; break;
				case 2:box[i].w=box[t].w+1; box[i].h=box[t].h; break;
				case 3:box[i].w=box[t].w; box[i].h=box[t].h+1; break;
			}
		}
		minw=minh=202;
		maxw=maxh=-202;
		for(i=0;i<n;i++){
			if(box[i].w<minw)minw=box[i].w;
			if(box[i].w>maxw)maxw=box[i].w;
			if(box[i].h<minh)minh=box[i].h;
			if(box[i].h>maxh)maxh=box[i].h;
		}
		printf("%d %d\n",maxw-minw+1,maxh-minh+1);
	}
	return 0;
}