while true
    n = gets.to_i
    break if n == 0
    tiles = [[0, 0]]
    left = right = top = bottom = 0
    (n - 1).times{
        a, b = gets.chomp.split.map(&:to_i)
        x, y = tiles[a]
        case b
        when 0
            x -= 1
            left = x if left > x
        when 1
            y -= 1
            bottom = y if bottom > y
        when 2
            x += 1
            right = x if right < x
        when 3
            y += 1
            top = y if top < y
        end
        tiles << [x, y]
    }
    puts "#{right - left + 1} #{top - bottom + 1}"
end
