#include<stdio.h>

typedef struct b{
  int u,l,r,d;
}BOX;

int main(void)
{
  int i;
  int n;
  int pos,dir;
  int h,w;
  BOX box[200];
  int maxu,maxl,maxr,maxd;


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

    maxu=0;maxl=0;maxr=0;maxd=0;
    for(i=0;i<n;i++){
      if(i==0){
	box[0].u=0;
	box[0].l=0;
	box[0].r=0;
	box[0].d=0;
      }else{
	scanf("%d %d",&pos,&dir);
	if(dir==0){
	  box[i].u=box[pos].u;
	  box[i].l=box[pos].l+1;
	  box[i].r=box[pos].r-1;
	  box[i].d=box[pos].d;
	  if(maxl<box[i].l)maxl=box[i].l;
	}else if(dir==1){
	  box[i].u=box[pos].u-1;
	  box[i].l=box[pos].l;
	  box[i].r=box[pos].r;
	  box[i].d=box[pos].d+1;
	  if(maxd<box[i].d)maxd=box[i].d;
	}else if(dir==2){
	  box[i].u=box[pos].u;
	  box[i].l=box[pos].l-1;
	  box[i].r=box[pos].r+1;
	  box[i].d=box[pos].d;
	  if(maxr<box[i].r)maxr=box[i].r;
	}else{
	  box[i].u=box[pos].u+1;
	  box[i].l=box[pos].l;
	  box[i].r=box[pos].r;
	  box[i].d=box[pos].d-1;
	  if(maxu<box[i].u)maxu=box[i].u;
	}
      }
    }

    w=maxl+maxr+1;
    h=maxu+maxd+1;

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

  return 0;
}