#include <stdio.h>
#include <stdlib.h>


int main(int argc,char *argv[]){
	int a[400],d[200][2],i,j,k,ansWidth,ansHeight,N;
	int right,left,up,bottom;
	while(1){
		right = 0;
		left = 0;
		up = 0;
		bottom = 0;
				
		scanf("%d",&N);
		if(N == 0){
			break;
		}			
		for(i = 0;i < N-1;i++){
			scanf("%d %d",&a[i*2],&a[i*2+1]);			
		}
		//printf("%d\n",N);
		//for(i = 0;i < N-1;i++){
		//	printf("%d %d\n",a[i*2],a[i*2+1]);
		//}

		//????\?
		d[0][0] = 0;
		d[0][1] = 0;
		for(i = 0;i < N-1;i++){
			switch(a[i*2+1]){
				case 0:
					d[i+1][0] = d[a[i*2]][0]-1;
					d[i+1][1] = d[a[i*2]][1];
					break;
				case 1:
					d[i+1][0] = d[a[i*2]][0];
					d[i+1][1] = d[a[i*2]][1]-1;
					break;
				case 2:
					d[i+1][0] = d[a[i*2]][0]+1;
					d[i+1][1] = d[a[i*2]][1];
					break;
				case 3:
					d[i+1][0] = d[a[i*2]][0];
					d[i+1][1] = d[a[i*2]][1]+1;
					break;
								
			}
			
		}
			
		for(i = 0;i < N;i++){
			printf("(%d,%d)\n",d[i][0],d[i][1]);
			if(d[i][0] > right){
				right = d[i][0];
			}else if(d[i][0] < left){
				left = d[i][0];
			}else if(d[i][1] > up){
				up = d[i][1];
			}else if(d[i][1] < bottom){
				bottom = d[i][1];
			}
		
		}
		printf("%d %d\n",right - left+1,up-bottom+1);
		//????\??????????
		
	}



	return 0;
}