object Main {
  val dir = List((-1, 0), (0, 1), (1, 0), (0, -1))
  def main(args:Array[String]) = {
    val sc = new java.util.Scanner(System.in)
    var n = sc.nextInt
    while(n != 0) {
      val pos = for(_ <- 1 to n - 1) yield (sc.nextInt, sc.nextInt)
      val poses = scala.collection.mutable.ArrayBuffer.empty[(Int, Int)]
      poses += ((0, 0))
      for(p <- pos) {
        poses += ((poses(p._1)._1 + dir(p._2)._1, poses(p._1)._2 + dir(p._2)._2))
      }
      val rangeX = poses.map(p => p._1).max - poses.map(p => p._1).min + 1
      val rangeY = poses.map(p => p._2).max - poses.map(p => p._2).min + 1
      println(List(rangeX, rangeY).mkString(" "))
      n = sc.nextInt
    }
  }
}