#include <stdio.h>

#define N_MAX 200

int main(int argc, char* argv[]){
	int N; FILE *file; file=fopen(argv[1],"r");
	
	while(fscanf(file,"%d\n",&N), N!=0){
		int min[2]={0,0}, max[2]={0,0}, i, array[2][N_MAX];
		
		array[1][0]=0, array[0][0]=0;
		
		for(i=1; i<N; i++){
			int n, d, j; fscanf(file,"%d %d\n",&n,&d);
			switch(d){
				case 0: array[0][i]=array[0][n]-1; array[1][i]=array[1][n]; break;
				case 1: array[1][i]=array[1][n]+1; array[0][i]=array[0][n]; break;
				case 2: array[0][i]=array[0][n]+1; array[1][i]=array[1][n]; break;
				case 3: array[1][i]=array[1][n]-1; array[0][i]=array[0][n]; break;
			}
			for(j=0; j<2; j++){
				if(array[j][i]<min[j]) min[j]=array[j][i];
				if(array[j][i]>max[j]) max[j]=array[j][i];
			}
		}
		
		printf("%d %d\n",max[0]-min[0]+1,max[1]-min[1]+1);
	}
	
}