loop do
  n = gets.to_i
  break if n == 0

  points = [[0, 0]]
  directions = [
    [-1, 0],
    [0, 1],
    [1, 0],
    [0, -1]
  ]
  (n-1).times do
    neighbor, d = gets.split.map(&:to_i)
    # p [neighbor, d]
    points << [ points[neighbor][0] + directions[d][0], points[neighbor][1] + directions[d][1] ]
  end

  min_x = points.min_by { |po| po[0] }[0]
  max_x = points.max_by { |po| po[0] }[0]
  min_y = points.min_by { |po| po[1] }[1]
  max_y = points.max_by { |po| po[1] }[1]

  puts "#{max_x - min_x + 1} #{max_y - min_y + 1}"
end