#include <iostream>
#include <vector>
using namespace std;

int N;
int dx[4] = {0,1,0,-1},dy[4] = {-1,0,1,0};
int main(){
    while(cin >> N && N){
        vector<int> X(N+1,0),Y(N+1,0);
        int n,d;
        for(int i=1;i<=N-1;i++){
            cin >> n >> d;
            X[i] = X[n]+dx[d];
            Y[i] = Y[n]+dy[d];
        }
        int xmin = 1e9,xmax = -1e9,ymin = 1e9,ymax = -1e9;
        for(int i=0;i<=N-1;i++){
            xmin = min(xmin,X[i]);
            xmax = max(xmax,X[i]);
            ymin = min(ymin,Y[i]);
            ymax = max(ymax,Y[i]);
        }
        cout << ymax-ymin+1 << " " << xmax-xmin+1 << endl;
    }
}
