import std.algorithm;
import std.conv;
import std.stdio;
import std.string;
import std.typecons;
 
alias Tuple!(int, "x", int, "y") Point;
 
auto move = [[-1, 0], [0, 1], [1, 0], [0, -1]];

void main(){
    string input;
    Point[200] points;
    points[0] = Point(0, 0);
    while((input = readln.chomp) != "0"){
        int n = input.to!int;
        if(n == 1){
            "1 1".writeln;
            continue;
        }
        points[1..n] = Point.init;
        foreach(i; 1..n){
            auto line = readln.split;
            int base = line[0].to!int;
            int dir = line[1].to!int;
            points[i] = Point(points[base].x + move[dir][0], points[base].y + move[dir][1]);
        }
        auto result = reduce!(q{max(a, b.x)}, q{min(a, b.x)}, q{max(a, b.y)}, q{min(a, b.y)})(tuple(0, 0, 0, 0), points[0..n]);
        writeln(result[0] - result[1] + 1, " ", result[2] - result[3] + 1);
    }
}