#include <stdio.h>
#include <string.h>

int main(void){
  int N;
  int n[200], d[200];
  int i,j,k;
  int W,H;
  int result[400][400];
  int max_W, min_W;
  int max_H, min_H;

  while(1){
    scanf("%d", &N);
    if(N==0) break;

    memset(n, 0, sizeof(n));
    memset(d, 0, sizeof(d));
    memset(result, -1, sizeof(result));

    result[200][200]=0;
    max_W=200, min_W=200;
    max_H=200, min_H=200;

    for(i=0; i<N-1; i++){
      scanf("%d %d", &n[i], &d[i]);
    }

    for(k=0; k<N-1; k++){
      for(i=0; i<400; i++){
        for(j=0; j<400; j++){
          if(n[k]==result[i][j]){
            if(d[k]==0){
              result[i][j-1]=k+1;
            }
            else if (d[k]==1){
              result[i+1][j]=k+1;
            }
            else if (d[k]==2){
              result[i][j+1]=k+1;
            }
            else if (d[k]==3){
              result[i-1][j]=k+1;
            }
          }
        }
      }
    }
    for(k=0; k<=N-1; k++){
      for(i=0; i<400; i++){
        for(j=0; j<400; j++){
          if(result[i][j]==k){
            if(i>=max_H) max_H=i;
            if(i<=min_H) min_H=i;
            if(j>=max_W) max_W=j;
            if(j<=min_W) min_W=j;
          }
        }
      }
    }

  W=max_W-min_W+1;
  H=max_H-min_H+1;

    printf("%d %d\n", W, H );
  }
  return 0;
}