### constant

DXYS = [[-1, 0], [0, 1], [1, 0], [0, -1]]

### main

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

  ops = (n - 1).times.map{gets.split.map(&:to_i)}

  sqs = [[0, 0]]
  min_x = max_x = min_y = max_y = 0

  ops.each do |ni, di|
    xi = sqs[ni][0] + DXYS[di][0]
    yi = sqs[ni][1] + DXYS[di][1]
    sqs << [xi, yi]

    min_x = xi if min_x > xi
    max_x = xi if max_x < xi
    min_y = yi if min_y > yi
    max_y = yi if max_y < yi
  end
  #p [min_x, max_x, min_y, max_y]

  w = max_x - min_x + 1
  h = max_y - min_y + 1
  puts [w, h].join(' ')
end