while (square_count = gets.to_i) != 0 do
  orders = []
  (square_count-1).times do
    target_square, direction =gets.split.map(&:to_i)
    orders.push([target_square, direction])
  end

  positions = [[0, 0]]
  orders.each do |order|
    #p 'order', order
    ref_position = positions[order[0]]
    case order[1]
      when 0
        positions.push([ref_position[0] -1, ref_position[1]])
      when 1
        positions.push([ref_position[0], ref_position[1] -1])
      when 2
        positions.push([ref_position[0] +1, ref_position[1]])
      when 3
        positions.push([ref_position[0], ref_position[1] +1])
    end

  end

  max_x = min_x = max_y = min_y = 0

  positions.each do |position|
    max_x = position[0] if position[0] >= max_x
    min_x = position[0] if position[0] <= min_x

    max_y = position[1] if position[1] >= max_y
    min_y = position[1] if position[1] <= min_y
  end
  
  width =max_x-min_x+1
  height = max_y-min_y + 1

  puts width.to_s + ' ' + height.to_s

end