#include<bits/stdc++.h>
using namespace std;

int dx[4]={-1,0,1,0}, dy[4]={0,1,0,-1};

int main(){
    int N;
    while(cin >> N, N != 0){
        vector<pair<int, int> > v;
        v.push_back(make_pair(0, 0));
        for(int i=1;i<N;++i){
            int n, d;
            cin >> n >> d;
            v.push_back(make_pair(v[n].first+dx[d], v[n].second+dy[d]));
        }
        int xmin = 0, ymin = 0, xmax = 0, ymax = 0;
        for(auto e : v){
            xmin = min(xmin, e.first);
            ymin = min(ymin, e.second);
            xmax = max(xmax, e.first);
            ymax = max(ymax, e.second);
        }
        cout << abs(xmax - xmin) + 1 << " " << abs(ymax - ymin) + 1 << endl;
    }

    return 0;
}
