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

int main(void){
  int N,i,n,d;
  int Xmin,Xmax,Ymin,Ymax;
  //←...0/↓...1/→...2/↑...3//
  while(scanf("%d",&N)!=EOF){
    if(N==0){break;}

    int x[200]={},y[200]={};
    Xmin=x[0];Xmax=x[0];Ymin=y[0];Ymax=y[0];
    for(i=1;i<N;i++){
      scanf("%d %d",&n,&d);
      switch(d){
        case 0:
          x[i]=x[n]-1;
          y[i]=y[n];
          break;
        case 1:
          x[i]=x[n];
          y[i]=y[n]-1;
          break;
        case 2:
          x[i]=x[n]+1;
          y[i]=y[n];
          break;
        case 3:
          x[i]=x[n];
          y[i]=y[n]+1;
          break;
        }
        if(Xmin>=x[i]) Xmin=x[i];
        if(Xmax<=x[i]) Xmax=x[i];
        if(Ymin>=y[i]) Ymin=y[i];
        if(Ymax<=y[i]) Ymax=y[i];
    }
    printf("%d %d\n",abs(Xmin-Xmax)+1,abs(Ymin-Ymax)+1);
  }
  return 0;
}

