#include <stdio.h>

int main(void) {
  int a[400][400];
  int N, i, j, k, x, y, left, right, front, back;
  int n[201], d[201];
  while (1) {
    scanf("%d", &N);
    if (N == 0) return 0;
    
    for (i = 1; i < N; i++)
      scanf("%d%d", &n[i], &d[i]);
    for (i = 0; i < 400; i++)
      for (j = 0; j < 400; j++)
	a[i][j] = -1;

    a[200][200] = 0;
    for (i = 1; i < N; i++) {
      for (k = 0; k < 400; k++)
	for (j = 0; j < 400; j++)
	  if (a[j][k] == n[i]) goto loop_end;
    loop_end: ;
      x = k;
      y = j;
      if (d[i] == 0) a[y][x - 1] = i;
      else if (d[i] == 1) a[y + 1][x] = i;
      else if (d[i] == 2) a[y][x + 1] = i;
      else a[y - 1][x] = i;
    }

    for (j = 0; j < 400; j++)
      for (i = 0; i < 400; i++)
	if (a[i][j] != -1) {
	  left = j;
	  break;
	}

    for (i = 0; i < 400; i++)
      for (j = 0; j < 400; j++)
	if (a[i][j] != -1) {
	  front = i;
	  break;
	}

    for (j = 399; j >= 0; j--)
      for (i = 399; i >= 0; i--)
	if (a[i][j] != -1) {
	  right = j;
	  break;
	}

    for (i = 399; i >= 0; i--)
      for (j = 399; j >= 0; j--)
	if (a[i][j] != -1) {
	  back = i;
	  break;
	}

    printf("%d %d\n", left - right + 1, front - back + 1);
  }
}