#include <stdio.h>

typedef struct {
	int x,y;
} xy_t;

int card_num;
xy_t cards[200];

int main(void) {
	int i;
	int from,direction;
	int min_x,max_x,min_y,max_y;
	while(1) {
		scanf("%d",&card_num);
		if(card_num==0)break;
		min_x=max_x=min_y=max_y=0;
		cards[0].x=cards[0].y=0;
		for(i=1;i<card_num;i++) {
			scanf("%d%d",&from,&direction);
			switch(direction) {
				case 0:
					cards[i].x=cards[from].x-1;
					cards[i].y=cards[from].y;
					break;
				case 1:
					cards[i].x=cards[from].x;
					cards[i].y=cards[from].y+1;
					break;
				case 2:
					cards[i].x=cards[from].x+1;
					cards[i].y=cards[from].y;
					break;
				case 3:
					cards[i].x=cards[from].x;
					cards[i].y=cards[from].y-1;
					break;
			}
			if(cards[i].x<min_x)min_x=cards[i].x;
			if(cards[i].x>max_x)max_x=cards[i].x;
			if(cards[i].y<min_y)min_y=cards[i].y;
			if(cards[i].y>max_y)max_y=cards[i].y;
		}
		printf("%d %d\n",max_x-min_x+1,max_y-min_y+1);
	}
	return 0;
}