#include <stdio.h>
main(){
  int i,N,fx[200],fy[200],num,xmax,xmin,ymax,ymin,d;
  int dx[4] = {-1,0,1,0};
  int dy[4] = {0,1,0,-1};
  fx[0] = 0;
  fy[0] = 0;

  while(1){
    scanf("%d",&N);
    if(N == 0) break;
    
    for(i=1;i<N;i++){
      scanf("%d%d",&num,&d);
      fx[i] = fx[num] + dx[d];
      fy[i] = fy[num] + dy[d];
    }

    xmax = 0;
    xmin = 0;
    ymax = 0;
    ymin = 0;

    for(i=0;i<N;i++){
      if(fx[i] > xmax) xmax = fx[i];
      if(fx[i] < xmin) xmin = fx[i];
      if(fy[i] > ymax) ymax = fy[i];
      if(fy[i] < ymin) ymin = fy[i];
    }

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