# coding: utf-8
Pos = Struct.new(:x, :y)
LEFT = 0
DOWN = 1
RIGHT = 2
UP = 3
while true do
  # coding: utf-8
  rect_num = gets.to_i
  if rect_num == 0
   break
  end
  # ???,????????£?????????
  max_pos = {up:0, down:0,right:0,left:0}
  poses = []
  poses.push(Pos.new(0,0))
  # ???????????¨?????????
  (rect_num - 1).times do
    line = gets
    # ????????§????????£???????????????????????????????????????
    parent_index,rect_direction = line.split
    parent_index = parent_index.to_i
    rect_direction = rect_direction.to_i
    parent = poses[parent_index]
    x = 0
    y = 0
    case rect_direction
    when UP then
      y = 1
    when RIGHT then
      x = 1
    when DOWN then
      y = -1
    when LEFT then
      x = -1
    end
    new_pos = Pos.new(parent[:x] + x, parent[:y] + y)
    poses.push(new_pos)
    max_pos[:right] = new_pos[:x] if new_pos[:x] > max_pos[:right]
    max_pos[:left] = new_pos[:x] if new_pos[:x] < max_pos[:left]
    max_pos[:up] = new_pos[:y] if new_pos[:y] > max_pos[:up]
    max_pos[:down] = new_pos[:y] if new_pos[:y] < max_pos[:down]
  end
  width = (max_pos[:right] - max_pos[:left]) + 1
  height = (max_pos[:up] - max_pos[:down]) + 1
  puts "#{width} #{height}"
end