
loop {
	n = gets.to_i
	break if n == 0
	squares = [[0, 0]]
	min_x = min_y = max_x = max_y = 0
	(n-1).times{
		i, d = gets.split.map &:to_i
		y, x = squares[i]
		case d
		when 0
			x -= 1
			min_x = x if x < min_x
		when 1
			y += 1
			max_y = y if y > max_y
		when 2
			x += 1
			max_x = x if x > max_x
		when 3
			y -= 1
			min_y = y if y < min_y
		end
		squares << [y, x]
	}
	puts "#{max_x - min_x + 1} #{max_y - min_y + 1}"
}