import scala.annotation.tailrec
object Main extends App {
  import scala.io.StdIn._
  solve
  @tailrec def solve: Unit = {
    val n = readLine.trim.toInt
    if (n == 0) return
    val squares = Array.tabulate(n){_ ⇒ Square(0, 0)}
    for (i ← 1 until n){
      val Array(a, b) = readLine.trim.split(' ').map(_.toInt)
      squares(i) = squares(a).neighbor(b)
    }
    println(
      s"${squares.map{_.x}.max - squares.map{_.x}.min + 1} ${squares.map{_.y}.max - squares.map{_.y}.min + 1}"
    )
    solve
  }
  implicit class ScopeFunc[T](private val value: T) {
    def let[A](func:(T) ⇒ A): A = func(value)
    def apply(func:(T)⇒Unit): T = {
      func(value)
      value
    }
  }
  case class Square(x:Int, y: Int){
    def neighbor(direction:Int): Square = direction match{
      case 0 ⇒ Square(x - 1, y)
      case 1 ⇒ Square(x, y - 1)
      case 2 ⇒ Square(x + 1, y)
      case 3 ⇒ Square(x, y + 1)
      case _ ⇒ throw new Exception
    }
  }
}
