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

  map = [[0,0]]
  (n-1).times do
    a,b = gets.split.map(&:to_i)

    case b
    when 0
      map << [map[a][0]-1,map[a][1]]
    when 1
      map << [map[a][0],map[a][1]-1]
    when 2
      map << [map[a][0]+1,map[a][1]]
    when 3
      map << [map[a][0],map[a][1]+1]
    end
  end
  w,h = 0,0
  map.map{|x,y|x}.minmax.tap{|mi,ma| w = ma - mi}
  map.map{|x,y|y}.minmax.tap{|mi,ma| h = ma - mi}
  puts [w+1,h+1].join(" ")
end