loop do
    n = gets.to_i
    break if n == 0
    pos = Array.new(n)
    pos[0] = [0,0]
    r = l = t = b = 0
    (n-1).times do |i|
        a = gets.split.map(&:to_i)
        pp = pos[a[0]]
        case a[1]
        when 0
            pos[i+1] = [pp[0]-1, pp[1]]
        when 1
            pos[i+1] = [pp[0], pp[1]-1]
        when 2
            pos[i+1] = [pp[0]+1, pp[1]]
        when 3
            pos[i+1] = [pp[0], pp[1]+1]
        end
        r = [r, pos[i+1][0]].max
        l = [l, pos[i+1][0]].min
        t = [t, pos[i+1][1]].max
        b = [b, pos[i+1][1]].min
    end
    puts "#{r-l+1} #{t-b+1}"
end