#define DEBUG 0
#define L -1
#define B -1
#define R 1
#define T 1

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>

struct sq_data{
	int no;
	int x;
	int y;
};



int main(){
	int N;
	
	while(1){
		if(DEBUG)printf("input N:");
		
		scanf("%d",&N);
		if(N == 0){
			break;
		}
		struct sq_data sq[N];
		sq[0].no = 0;
		sq[0].x = 0;
		sq[0].y = 0;
		
		int i;
		int n,d;
		for(i = 1;i < N;i++){
			scanf("%d%d",&n,&d);
			if(d == 0){//左
				sq[i].x = sq[n].x + L;
				sq[i].y = sq[n].y;
			}
			else if(d == 1){//下
				sq[i].x = sq[n].x;
				sq[i].y = sq[n].y + B;
			}
			else if(d == 2){//右
				sq[i].x = sq[n].x + R;
				sq[i].y = sq[n].y;
			}
			else if(d == 3){//上
				sq[i].x = sq[n].x;
				sq[i].y = sq[n].y + T;
			}
		}
		
		int buf_min_x=0;
		int buf_max_x=0;
		int buf_min_y=0;
		int buf_max_y=0;
		for(i = 0;i < N;i++){
			if(i == 0){
				buf_min_x = 0;
				buf_max_x = 0;
			}
			if(sq[i].x<buf_min_x){
				buf_min_x = sq[i].x;
			}
			else if(sq[i].x>buf_max_x){
				buf_max_x = sq[i].x;
			}
			
			if(sq[i].y<buf_min_y){
				buf_min_y = sq[i].y;
			}
			else if(sq[i].y>buf_max_y){
				buf_max_y = sq[i].y;
			}
		}
	
		
		printf("%d %d\n",buf_max_x-buf_min_x+1,buf_max_y-buf_min_y+1);
	}
	
	
	return 0;
}

// vim: se noet ts=4 sw=4 sts=0 ft=c :