#include <stdio.h>
struct point {int x;int y;};
struct point array[200];

int main(){
  int n,a,b,i,j,k,count;
  int xmax,xmin,ymax,ymin;
  while(1){
    scanf("%d",&n);
    xmax=xmin=ymax=ymin=0;
    if(n==0)
      break;
    if(n==1){
      printf("1 1\n");
    }else{
    
     array[0].x = 0;
     array[0].y = 0;
     for(i=1;i<n;i++){
	scanf("%d %d",&a,&b);
	switch(b){
	  case 0:
	    array[i].x = array[a].x-1;
	    array[i].y = array[a].y;
	    break;
	  case 1:
	    array[i].x = array[a].x;
   	    array[i].y = array[a].y -1;
	    break;
	  case 2:
	    array[i].x = array[a].x+1;
	    array[i].y = array[a].y;
	    break;
	  case 3:
	    array[i].x = array[a].x;
	    array[i].y = array[a].y+1;
	    break;  
	}
     }
     for(i=0;i<n;i++){
       if(array[i].x>xmax)
	 xmax=array[i].x;
       if(array[i].x<xmin)
	 xmin=array[i].x;
       if(array[i].y>ymax)
	 ymax=array[i].y;
       if(array[i].y<ymin)
	 ymin=array[i].y;
     }
     
     printf("%d %d\n",xmax-xmin+1,ymax-ymin+1);
     
    }
  }
  
  return 0;
}