import scala.annotation.tailrec

object Main extends App {

  override def main(args: Array[String]): Unit = {
    val answerMap = Solver.solveAll

    import scala.io.{StdIn => in}
    Iterator.continually(in.readLine()).takeWhile(_ != "0 0 0").map(_.split(" ")).map(arr => new Solver(arr(0).toInt, arr(1).toInt, arr(2).toInt))
      .map(solver => solver.solve(answerMap)).foreach(println)
  }

}


sealed trait Coordination {
  val x: Int
  val y: Int
}

object Coordination {
  def apply(x: Int, y: Int): Coordination =
    if (((x + y) & 1) == 1) OddCoordination(x, y)
    else EvenCoordination(x, y)

}

case class OddCoordination(x: Int, y: Int) extends Coordination {}

case class EvenCoordination(x: Int, y: Int) extends Coordination {}

case class Face(buttom: Int, southOrNoth: Int)

sealed trait CoordinationAndFace {
  val coordination: Coordination
  val face: Face

  def next: Seq[CoordinationAndFace]

  val toKeySring:String = List(coordination.x,coordination.y,face.buttom,face.southOrNoth).mkString(",")
}

object CoordinationAndFace {
  def apply(coordination: Coordination, face: Face): CoordinationAndFace = {
    coordination match {
      case e: EvenCoordination => EvenCoordinationAndFace(e, face)
      case o: OddCoordination => OddCoordinationAndFace(o, face)
    }
  }
}

case class EvenCoordinationAndFace(coordination: EvenCoordination, face: Face) extends CoordinationAndFace {
  override def next = Seq(toNorth, toSouthEast, toSouthWest)

  val now = IcosahedronFaces(face.buttom)(face.southOrNoth)

  def toNorth: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x, coordination.y + 1)
    val nextFace = IcosahedronFaces(now.top)(now.n)
    val newFace = Face(nextFace.n, nextFace.top)
    CoordinationAndFace(newCoordination, newFace)
  }

  def toSouthWest: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x - 1, coordination.y)
    val nextFace = IcosahedronFaces(now.right)(now.n)
    val newFace = Face(nextFace.n, nextFace.left)
    CoordinationAndFace(newCoordination, newFace)
  }

  def toSouthEast: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x + 1, coordination.y)
    val nextFace = IcosahedronFaces(now.left)(now.n)
    val newFace = Face(nextFace.n, nextFace.right)
    CoordinationAndFace(newCoordination, newFace)
  }
}

case class OddCoordinationAndFace(coordination: OddCoordination, face: Face) extends CoordinationAndFace {
  override def next = Seq(toSouth, toNorthEast, toNorthWest)

  val now = IcosahedronFaces(face.buttom)(face.southOrNoth)

  def toSouth: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x, coordination.y - 1)
    val nextFace = IcosahedronFaces(now.top)(now.n)
    val newFace = Face(nextFace.n, nextFace.top)
    CoordinationAndFace(newCoordination, newFace)
  }

  def toNorthWest: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x - 1, coordination.y)
    val nextFace = IcosahedronFaces(now.left)(now.n)
    val newFace = Face(nextFace.n, nextFace.right)
    CoordinationAndFace(newCoordination, newFace)
  }

  def toNorthEast: CoordinationAndFace = {
    val newCoordination = Coordination(coordination.x + 1, coordination.y)
    val nextFace = IcosahedronFaces(now.right)(now.n)
    val newFace = Face(nextFace.n, nextFace.left)
    CoordinationAndFace(newCoordination, newFace)
  }

}

sealed case class IcosahedronFace(n: Int, top: Int, left: Int, right: Int) {
  def apply(asTop: Int): IcosahedronFace = {
    asTop match {
      case `top` => this
      case `left` => IcosahedronFace(n, left, right, top)
      case `right` => IcosahedronFace(n, right, top, left)
    }
  }
}

object IcosahedronFaces {
  private val list = List(
    IcosahedronFace(0, 5, 1, 4),
    IcosahedronFace(1, 6, 2, 0),
    IcosahedronFace(2, 7, 3, 1),
    IcosahedronFace(3, 8, 4, 2),
    IcosahedronFace(4, 9, 0, 3),
    IcosahedronFace(5, 0, 10, 11),
    IcosahedronFace(6, 1, 11, 12),
    IcosahedronFace(7, 2, 12, 13),
    IcosahedronFace(8, 3, 13, 14),
    IcosahedronFace(9, 4, 14, 10),
    IcosahedronFace(10, 15, 5, 9),
    IcosahedronFace(11, 16, 6, 5),
    IcosahedronFace(12, 17, 7, 6),
    IcosahedronFace(13, 18, 8, 7),
    IcosahedronFace(14, 19, 9, 8),
    IcosahedronFace(15, 10, 19, 16),
    IcosahedronFace(16, 11, 15, 17),
    IcosahedronFace(17, 12, 16, 18),
    IcosahedronFace(18, 13, 17, 19),
    IcosahedronFace(19, 14, 18, 15)
  )

  def apply(n: Int): IcosahedronFace = n match {
    case n: Int if ((0 to 19).contains(n)) => list(n)
  }
}


class Solver(x: Int, y: Int, n: Int) {
  def solve(answerMap: Map[String, Int]): Int = {
    val coordination = Coordination(x, y)
    val missing = coordination match {
      case e: EvenCoordination => 100
      case o: OddCoordination => 99
    }

    val iface = IcosahedronFaces(n)

    val keys = List(
      CoordinationAndFace(coordination, Face(n, iface.top)),
      CoordinationAndFace(coordination, Face(n, iface.left)),
      CoordinationAndFace(coordination, Face(n, iface.right))
    )

    keys.map(cf => answerMap.getOrElse(cf.toKeySring, missing)).min
  }

}

object Solver {


  def solveAll: Map[String, Int] = {
    val init: CoordinationAndFace = CoordinationAndFace(Coordination(0, 0), Face(0, 5))

    var (map: Map[String, Int], queue: Seq[(CoordinationAndFace, Int)]) = (Map(init.toKeySring -> 0), Vector(init -> 0))
    do {
      val res = solveInner(map,queue)
      map = res._1
      queue = res._2
    }while(queue.head._2 < 98)
    map
  }

  private def solveInner(map: Map[String, Int], queue: Seq[(CoordinationAndFace, Int)]): (Map[String, Int], Seq[(CoordinationAndFace, Int)]) =
    queue match {
      case Vector(head, rest@_*) if (head._2 < 98) =>
        val candidate = head._1.next
        val nextCost = head._2 + 1
        val (newMap, newQueue) =
          candidate.filterNot(cf => map.contains(cf.toKeySring)).foldLeft((map, rest))((mq, next) => {
            (mq._1 + (next.toKeySring -> nextCost), mq._2 :+ (next, nextCost))
          })
        (newMap, newQueue)
      case _ => (map, queue)
    }
}