#include<bits/stdc++.h>
using namespace std;
int main(){
  int n,x,y;
  while(cin>>n&&n){
    vector<int> w(n),h(n);
    w[0]=h[0]=0;
    for(int i=1;i<n;++i){
      cin>>x>>y;
      switch(y){
      case 0:
	w[i]=w[x]-1;
	h[i]=h[x];
	break;
      case 1:
	w[i]=w[x];
	h[i]=h[x]-1;
	break;
      case 2:
	w[i]=w[x]+1;
	h[i]=h[x];
	break;
      case 3:
	w[i]=w[x];
	h[i]=h[x]+1;
      }
    }
    sort(w.begin(),w.end());
    sort(h.begin(),h.end());
    int maxw=w[n-1];
    int minw=w[0];
    int maxh=h[n-1];
    int minh=h[0];
    cout<<maxw-minw+1<<" "<<maxh-minh+1<<endl;
  }
  return 0;
}

