#include <bits/stdc++.h>

using namespace std;
int dx[] = {-1, 0, 1 , 0};
int dy[] = {0, -1, 0, 1};

int main(){
	while(true){
		int n; cin >> n;
		if(n == 0)break;
		vector<pair<int ,int> > a(n);
		a[0] = make_pair(0,0);
		for(int i = 1; i < n; i++){
			int m, d; cin >> m >> d;
			a[i] = make_pair(a[m].first + dx[d], a[m].second + dy[d]);
		}
		int xmin = (int)1e9,xmax = -(int)1e9,ymin = (int)1e9,ymax = -(int)1e9;
		for(int i = 0; i < n; i++){
			xmin = min(xmin, a[i].first);
			xmax = max(xmax, a[i].first);
			ymin = min(ymin, a[i].second);
			ymax = max(ymax, a[i].second);
		}
		xmax -= xmin; ymax -= ymin;
		xmax++; ymax++;
		cout << xmax << " " << ymax << endl;
	}

	return 0;
}


