#include <stdio.h>

int main(void){

	int N;
	int x[200];
	int y[200];
	int i,n,d,max,min;
	while(scanf("%d",&N),N){
		x[0]=0;
		y[0]=0;
		for(i=1;i<N;i++){
			scanf("%d %d",&n,&d);
			switch(d){
				case 0:
					x[i]=x[n]-1;
					y[i]=y[n];
					break;
				case 1:
					x[i]=x[n];
					y[i]=y[n]-1;
					break;
				case 2:
					x[i]=x[n]+1;
					y[i]=y[n];
					break;
				case 3:
					x[i]=x[n];
					y[i]=y[n]+1;
					break;
			}
		}
		max=min=0;
		for(i=0;i<N;i++){
			if(x[i]>max)max=x[i];
			if(x[i]<min)min=x[i];
		}
		printf("%d ", max-min+1);
		max=min=0;
		for(i=0;i<N;i++){
			if(y[i]>max)max=y[i];
			if(y[i]<min)min=y[i];
		}
		printf("%d\n", max-min+1);
	}
	return 0;
}