import std.stdio;
import std.conv;
import std.complex;
import std.algorithm;
import std.range;
import std.math;
import std.string;


void main()
{
    while(1){
        immutable int N = readln().chomp.to!uint;
        if(N == 0)
            break;
 
        Complex!real[] map2D = new Complex!real[N];
        map2D[0] = complex(0, 0);
 
        {
            immutable cpxDir = [complex(-1, 0), complex(0, -1), complex(1, 0), complex(0, 1)];
            size_t i = 1;
            foreach(e; iota(N-1).map!(a => readln.split[0..2].map!(to!uint)))
                map2D[i++] = map2D[e[0]] + cpxDir[e[1]];
        }

        auto mm = [map2D.map!"a.re".reduce!(max, min),
                   map2D.map!"a.im".reduce!(max, min)];

        writefln("%s %s", abs(mm[0][0] - mm[0][1]).round.to!int + 1, abs(mm[1][0] - mm[1][1]).round.to!int + 1);
    }
}