#include <stdio.h>

struct grid {
	int x;
	int y;
};

struct grid area(int num) {
	struct grid grid[200] = { {0,0} };
	int next;
	int position;
	int left = 0;
	int right = 0;
	int top = 0;
	int bottom = 0;
	int count;

	for (count = 1; count < num; count++) {
		scanf("%d", &next);
		scanf("%d", &position);
		switch (position) {
		case 0:grid[count].x = grid[next].x - 1;
			grid[count].y = grid[next].y;
			if (left > grid[count].x)
				left = grid[count].x;
			break;
		case 1:grid[count].x = grid[next].x;
			grid[count].y = grid[next].y - 1;
			if (bottom > grid[count].y)
				bottom = grid[count].y;
			break;
		case 2:grid[count].x = grid[next].x + 1;
			grid[count].y = grid[next].y;
			if (right < grid[count].x)
				right = grid[count].x;
			break;
		case 3:grid[count].x = grid[next].x;
			grid[count].y = grid[next].y + 1;
			if (top < grid[count].y)
				top = grid[count].y;
			break;
		}
	}
	grid[0].x = right - left + 1;
	grid[0].y = top - bottom + 1;
	return  grid[0];
}

int main(void) {
	int num;
	struct grid result;

	while (1) {
		scanf("%d", &num);
		if (num == 0)
			break;
		result = area(num);
		printf("%d %d\n", result.x, result.y);
	}
	return 0;
}
