#include <bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for (int64_t i = (a); i < (b); i++)
#define REP(i,n) rep(i,0,n)

void solve()
{
    int n;
    while(cin>>n,n){
        vector<int> x(1,0),y(1,0);x[0]=0;y[0]=0;
        int a,b;REP(i,n-1){
            cin>>a>>b;
            if(b==0)x.push_back(x[a]-1),y.push_back(y[a]);
            if(b==1)x.push_back(x[a]),y.push_back(y[a]-1);
            if(b==2)x.push_back(x[a]+1),y.push_back(y[a]);
            if(b==3)x.push_back(x[a]),y.push_back(y[a]+1);
        }
        sort(x.begin(),x.end());
        sort(y.begin(),y.end());
        cout<<x[x.size()-1]-x[0]+1<<" "<<y[y.size()-1]-y[0]+1<<endl;
    }
}

int main() 
{
    cin.tie(0);
	ios::sync_with_stdio(false);
    solve();
    return 0;
}
