#include <stdio.h>
#define ll long long
#define rep(i,l,r)for(ll i=l;i<r;i++)
#define max(p,q)((p)>(q)?(p):(q))
#define min(p,q)((p)<(q)?(p):(q))

ll d4[]={-1,0,1,0,-1};
ll x[210];
ll y[210];
int main(){
	ll n;
	while(scanf("%lld",&n),n){
		ll mx=0,Mx=0,my=0,My=0;
		rep(i,1,n){
			ll k,d;
			scanf("%lld%lld",&k,&d);
			x[i]=x[k]+d4[d];
			y[i]=y[k]+d4[d+1];
			mx=min(mx,x[i]);
			Mx=max(Mx,x[i]);
			my=min(my,y[i]);
			My=max(My,y[i]);
		}
		printf("%lld %lld\n",Mx-mx+1,My-my+1);
	}
}
