import std.stdio;
import std.algorithm;
import std.string;
import std.range;
import std.array;
import std.conv;
import std.complex;
import std.math;
import std.ascii;
import std.bigint;
import std.container;
import std.typecons;

auto readInts() {
	return array(map!(to!int)(readln().strip().split()));
}
auto readInt() {
	return readInts()[0];
}
auto readLongs() {
	return array(map!(to!long)(readln().strip().split()));
}
auto readLong() {
	return readLongs()[0];
}

const real eps = 1e-10;

void main(){
	while(true) {
		auto n = readInt();
		if(n == 0) {
			return;
		}
		auto c = [[0,0]];
		for(int i = 1; i < n; ++i) {
			auto nd = readInts();
			auto dx = [[-1, 0], [0, -1], [1, 0], [0, 1]];
			c ~= c[nd[0]].dup;
			c.back[] += dx[nd[1]][];
		}
		int b, u, l, r;
		foreach(ci; c) {
			b = min(b, ci[1]);
			u = max(u, ci[1]);
			l = min(l, ci[0]);
			r = max(r, ci[0]);
		}
		writeln(r-l+1, " ", u-b+1);
	}
}