#include <stdio.h>

int main() {

  int n;
  int a,b,c;
  int i,j,k;
  int w,h; 
  int u,l,r,d;

  while(1) {

  scanf("%d",&n);

 if(n == 0) break;


 int x[205] = {0};
 int y[205] = {0};

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

   
   if(b == 0  ) {
      x[i] = x[a]-1;
      y[i] = y[a];
    }else if(b == 1){
      x[i] = x[a];
      y[i] = y[a] - 1;
    } else if(b == 2) {
      x[i] = x[a] + 1;
      y[i] = y[a];
    } else if(b == 3){
      x[i] = x[a];
      y[i] = y[a] + 1;
    }
   }
 
 
  
 u = 0;
 d = 0;
 r = 0;
 l = 0;

  for(i=1;i<205;i++) {
   
	  if(x[i] > r) r = x[i];
	  if(x[i] < l) l = x[i];
	  if(y[i] > u) u = y[i];
	  if(y[i] < d) d = y[i];
	}

 
  if(l < 0) l = l *(-1);
  if(d < 0) d = d*(-1);

  w = r+l;
  h = u+d;


  printf("%d %d\n",w+1,h+1);

 }


  return 0;




}