#include <stdio.h>

typedef struct
{
	int x;
	int y;
} point;

int main(void)
{
	int N, ni, di;
	int i;
	int maxX = 0, minX = 0, maxY = 0, minY = 0;
	while(1)
	{
		scanf("%d", &N);
		if(N == 0) break;
		point P[N];
		P[0].x = 0; P[0].y = 0;
		for(i = 1; i < N; i++)
		{
			scanf("%d %d", &ni, &di);
			switch(di)
			{
				case 0:
					P[i].x = P[ni].x - 1;
					P[i].y = P[ni].y;
					break;
				case 1:
					P[i].x = P[ni].x;
					P[i].y = P[ni].y - 1;
					break;
				case 2:
					P[i].x = P[ni].x + 1;
					P[i].y = P[ni].y;
					break;
				case 3:
					P[i].x = P[ni].x;
					P[i].y = P[ni].y + 1;
					break;
			}
			if(P[i].x > maxX) maxX = P[i].x;
			if(P[i].x < minX) minX = P[i].x;
			if(P[i].y > maxY) maxY = P[i].y;
			if(P[i].y < minY) minY = P[i].y;
		}
		printf("%d %d\n", maxX - minX + 1, maxY - minY + 1);
		maxX = 0, minX = 0, maxY = 0, minY = 0;
	}

	return 0;
}

