import std.stdio, std.string, std.conv, std.algorithm, std.numeric;
import std.range, std.array, std.math, std.typecons, std.container, core.bitop;

alias Pair = Tuple!(int, "x", int, "y");

void main() {
    while (1) {
        int n;
        scan(n);

        if (!n) return;

        auto a = new Pair[](n);

        foreach (i ; 1 .. n) {
            int ni, di;
            scan(ni, di);
            if (di == 0) {
                a[i] = Pair(a[ni].x-1, a[ni].y);
            }
            else if (di == 1) {
                a[i] = Pair(a[ni].x, a[ni].y - 1);
            }
            else if (di == 2) {
                a[i] = Pair(a[ni].x+1, a[ni].y);
            }
            else {
                a[i] = Pair(a[ni].x, a[ni].y+1);
            }
        }

        int lm = 1000, rm = -1000;
        int bm = 1000, tm = -1000;

        foreach (ai ; a) {
            lm = min(lm, ai.x);
            rm = max(rm, ai.x);
            bm = min(bm, ai.y);
            tm = max(tm, ai.y);
        }

        writeln(rm - lm + 1, " ", tm - bm + 1);
    }
}


void scan(T...)(ref T args) {
    string[] line = readln.split;
    foreach (ref arg; args) {
        arg = line.front.to!(typeof(arg));
        line.popFront();
    }
    assert(line.empty);
}


void fillAll(R, T)(ref R arr, T value) {
    static if (is(typeof(arr[] = value))) {
        arr[] = value;
    }
    else {
        foreach (ref e; arr) {
            fillAll(e, value);
        }
    }
}