while true
	num=gets.to_i
	break if num == 0
	a=[[0,0]]
	count = 0
	(num-1).times{|t|
		n,d = gets.split(" ").map{|e|e.to_i}
		point = a[n]
		a.push [point[0]-1,point[1]] if d == 0
		a.push [point[0],point[1]-1] if d == 1
		a.push [point[0]+1,point[1]] if d == 2
		a.push [point[0],point[1]+1] if d == 3
	}
	maxh=maxw=minh=minw=0
	a.each{|e|
		maxw = e[0] if e[0] > maxw
		minw = e[0] if e[0] < minw
		maxh = e[1] if e[1] > maxh
		minh = e[1] if e[1] < minh
	}
	puts "#{maxw-minw+1} #{maxh-minh+1}"
end