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]];
        }


        real[2] iM = [0, 0], im = [0, 0];
        foreach(i, e; map2D){
            iM[0] = max(e.re, iM[0]);
            iM[1] = max(e.im, iM[1]);
            im[0] = min(e.re, im[0]);
            im[1] = min(e.im, im[1]);
        }

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