def ascan; gets.split.map(&:to_i);end

Ori = [[-1,0],[0,1],[1,0],[0,-1]]

while (n = gets.to_i) > 0
    top, bottom, left, right = 0,0,0,0
    pos = Array.new(n){[0,0]}
    (n-1).times do |_i|
        i = _i+1
        j, r = ascan
        
        o = Ori[r]
        pos[i] = [pos[j][0]+o[0], pos[j][1]+o[1]]
        
        top    = [pos[i][1], top].min
        bottom = [pos[i][1], bottom].max
        left   = [pos[i][0], left].min
        right  = [pos[i][0], right].max
    end
    puts [right-left+1, bottom-top+1]*" "
end

