//Number 1165

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
  int n, i, j, sq, dir, height_u, height_d, width_r, width_l;
  int pos[2][200];

  while(1)
    {
      height_u = 0;
      height_d = 0;
      width_r = 0;
      width_l = 0;
      for(i=0; i<2; i++)
	for(j=0; j<200; j++)
	  pos[i][j] = 0;
      
      scanf("%d", &n);
      if(n==0)
	break;

      for(i=1; i<n; i++)
	{
	  scanf("%d %d", &sq, &dir);

	  
	  if(dir==0)
	    {
	      pos[0][i] = pos[0][sq]-1;
	      pos[1][i] = pos[1][sq];
	      if(width_l<abs(pos[0][i]) && pos[0][i]<0)
		width_l = abs(pos[0][i]);
	    }
	  else if(dir==1)
	    {
	      pos[0][i] = pos[0][sq];
	      pos[1][i] = pos[1][sq]-1;
	      if(height_d<abs(pos[1][i]) && pos[1][i]<0)
		height_d = abs(pos[1][i]);
	    }
	  else if(dir==2)
	    {
	      pos[0][i] = pos[0][sq]+1;
	      pos[1][i] = pos[1][sq];
	      if(width_r<abs(pos[0][i]) && pos[0][i]>0)
		width_r = abs(pos[0][i]);
	    }
	  else if(dir==3)
	    {
	      pos[0][i] = pos[0][sq];
	      pos[1][i] = pos[1][sq]+1;
	      if(height_u<abs(pos[1][i]) && pos[1][i]>0)
		height_u = abs(pos[1][i]);
	    }
	}
      
      printf("%d %d\n", width_l+width_r+1, height_u+height_d+1);

    }
  return 0;
}