// Original C++ solution (slower):
#include <iostream>

#include <cstring>

#include <utility>

#include <vector>

#include <algorithm>

using namespace std;



#define MAXSIZE 500

#define CENTER (MAXSIZE / 2)



// ?????´(= 0)????????´ (= 1)????????´ (= 2)????????´ (= 3)

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

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



int tables[MAXSIZE][MAXSIZE];



inline int index(int n){

    return n + CENTER;

}



int main(int argc, const char * argv[]) {

    while(1){

        int n; cin >> n;

        if(!n) break;

        

        int left, right, top, bottom;

        left = right = top = bottom = CENTER;

        memset(tables, -1, sizeof(tables));

        

        vector<pair<int, int> > tableIndex;

        tableIndex.push_back(make_pair(CENTER, CENTER));

        

        for(int i = 1; i < n; i++){

            int m, d; cin >> m >> d;

            

            int x = tableIndex[m].first + dx[d];

            int y = tableIndex[m].second + dy[d];

            tableIndex.push_back(make_pair(x, y));

            

            left = min(left, x);

            right = max(right, x);

            top = min(top, y);

            bottom = max(bottom, y);

        }

        

        cout << right - left + 1 << " " << bottom - top + 1 << endl;

        

    }

}

// Optimized solution to the same problem (faster):
#include <iostream>

#include <cstring>

#include <utility>

#include <vector>

#include <algorithm>

using namespace std;



#define MAXSIZE 500

#define CENTER (MAXSIZE / 2)



// ?????´(= 0)????????´ (= 1)????????´ (= 2)????????´ (= 3)

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

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



int main(int argc, const char * argv[]) {

    while(1){

        int n; cin >> n;

        if(!n) break;

        

        int left, right, top, bottom;

        left = right = top = bottom = CENTER;

        

        vector<pair<int, int> > tableIndex;

        tableIndex.push_back(make_pair(CENTER, CENTER));

        

        for(int i = 1; i < n; i++){

            int m, d; cin >> m >> d;

            

            int x = tableIndex[m].first + dx[d];

            int y = tableIndex[m].second + dy[d];

            tableIndex.push_back(make_pair(x, y));

            

            left = min(left, x);

            right = max(right, x);

            top = min(top, y);

            bottom = max(bottom, y);

        }

        

        cout << right - left + 1 << " " << bottom - top + 1 << endl;

    }

}

// Fastest known solution for this problem:
#include<iostream>

#include<vector>

#include<algorithm>

#define loop(i,a,b) for(int i=a;i<b;i++)

#define rep(i,a) loop(i,0,a)

using namespace std;

typedef pair<int,int> pii;



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

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



int main(){

	int n;

	while(cin>>n,n){

		vector<pii> vec;

		vec.push_back(pii(0,0));

		int m,d;

		loop(i,1,n){

			cin>>m>>d;

			pii now=vec[m];

			now.first+=dx[d];

			now.second+=dy[d];

			vec.push_back(now);

		}

		int wmi=1e6,wma=-1*1e6,hmi=1e6,hma=-1*1e6;

		rep(i,vec.size()){

			wma=max(wma,vec[i].first);

			wmi=min(wmi,vec[i].first);

			hmi=min(hmi,vec[i].second);

			hma=max(hma,vec[i].second);

		}

		cout<<wma-wmi+1<<" "<<hma-hmi+1<<endl;

	}

	return 0;

}