#include <stdio.h>

int n;
int ni[200],di[200];
int x[200]={0},y[200]={0};
int mx[4]={-1,0,1,0};
int my[4]={0,-1,0,1};
int max,mix,may,miy;

int main(void){
  int i,j;
  while(1){
    scanf("%d",&n);
    if(n==0)break;
    for(i=1;i<n;i++)scanf("%d%d",&ni[i],&di[i]);
    max=0;
    mix=0;
    may=0;
    miy=0;
    for(i=1;i<n;i++){
      x[i]=x[ni[i]]+mx[di[i]];
      y[i]=y[ni[i]]+my[di[i]];
      if(x[i]>max)max=x[i];
      if(x[i]<mix)mix=x[i];
      if(y[i]>may)may=y[i];
      if(y[i]<miy)miy=y[i];
    }
    printf("%d %d\n",max-mix+1,may-miy+1);
  }
  return 0;
}
