#include <stdio.h>
main(){
  int i,j,n,a[401][401],ni,di,k,d[4],xmax,xmin,ymax,ymin,jud;
  while(1){
    scanf("%d",&n);
    if(n==0) break;
    xmax=200;
    xmin=200;
    ymax=200;
    ymin=200;
    for(i=0;i<401;i++){
      for(j=0;j<401;j++) a[i][j]=-1;
    }
    for(i=0;i<4;i++) d[i]=0;
    a[200][200]=0;

    for(i=1;i<n;i++){
      scanf("%d%d",&ni,&di);
      jud=0;
      for(j=200-d[0] ; j<=200+d[2] ; j++){
	for(k=200-d[1] ; k<=200+d[3] ; k++){
	  if(a[j][k] == ni){
	    if(di == 0){
	      a[j-1][k] = i;
	      if(j-1 < xmin){
		xmin = j-1;
		d[0]++;
	      }
	    }
	    else if(di == 1){
	      a[j][k-1] = i;
	      if(k-1 < ymin){
		ymin = k-1;
		d[1]++;
	      }
	    }
	    else if(di == 2){
	      a[j+1][k] = i;
	      if(j+1 > xmax){
		xmax = j+1;
		d[2]++;
	      }
	    }
	    else if(di == 3){
	      a[j][k+1] = i;
	      if(k+1 > ymax){
		ymax = k+1;
		d[3]++;
	      }
	    }
	    jud=1;
	    break;
	  }
	}
	if(jud == 1) break;
      }
    }


    printf("%d %d\n",xmax-xmin+1,ymax-ymin+1);
  }
  return 0;
}