/*
AizuOnline A1165
Title
*/
#include <stdio.h>
#define MAXRECTS 200
#define min(x,y) (((x)<(y))?(x):(y))
#define max(x,y) (((x)>(y))?(x):(y))
int n;
int op[MAXRECTS];
int rp[MAXRECTS];
int x[MAXRECTS];
int y[MAXRECTS];
int xmin,ymin,xmax,ymax;
//
main()
{
  int i,j;

  x[0]=y[0]=0;
  while(EOF!=scanf("%d",&n) && n)
    {
      for(i=1;i<n;i++)
	{
	  scanf("%d %d",&op[i],&rp[i]);
	  {
	    if(rp[i]==0)
	      {
		x[i]=x[op[i]]-1;
		y[i]=y[op[i]];
#ifdef DEBUG
		printf("%d %d %d\n",i,x[i],y[i]);
#endif
	      }
	    if(rp[i]==1)
	      {
		x[i]=x[op[i]];
		y[i]=y[op[i]]+1;
#ifdef DEBUG
		printf("%d %d %d\n",i,x[i],y[i]);
#endif

	      }
	    if(rp[i]==2)
	      {
		x[i]=x[op[i]]+1;
		y[i]=y[op[i]];
#ifdef DEBUG
		printf("%d %d %d\n",i,x[i],y[i]);
#endif

	      }
	    if(rp[i]==3)
	      {
		x[i]=x[op[i]];
		y[i]=y[op[i]]-1;

#ifdef DEBUG
		printf("%d %d %d\n",i,x[i],y[i]);
#endif
	      }
	  }
	}
      xmin=0;xmax=0;
      ymin=0;ymax=0;

	for(j=1;j<n;j++)
	  {
#ifdef DEBUG
	    printf("%d %d\n",x[j],y[j]);
#endif
	    xmin=min(xmin,x[j]);xmax=max(xmax,x[j]);
	    ymin=min(ymin,y[j]);ymax=max(ymax,y[j]);
	  }
      printf("%d %d\n" ,xmax-xmin+1,ymax-ymin+1);

    }
return(0);
}