// Aizu 1165: Pablo Squarson's Headache
// 2017.9.13 bal4u@uu

#include <stdio.h>

typedef struct { int r, c; } T;
T tbl[202];

int main()
{
	int n, i, m, d, r, c, r1, r2, c1, c2;

	while (scanf("%d", &n) && n) {
		tbl[0].r = tbl[0].c = r1 = r2 = c1 = c2 = 200;
		for (i = 1; i < n; i++) {
			scanf("%d%d", &m, &d);
			r = tbl[m].r, c = tbl[m].c;
			if      (d == 0) c--;
			else if (d == 1) r++;
			else if (d == 2) c++;
			else             r--;
			tbl[i].r = r, tbl[i].c = c;
			if (r < r1) r1 = r;
			if (r > r2) r2 = r;
			if (c < c1) c1 = c;
			if (c > c2) c2 = c;
		}
		printf("%d %d\n", c2-c1+1, r2-r1+1); 
	}
	return 0;
}