import scala.io.StdIn.{readInt,readLine}
import scala.collection.mutable.HashMap
import scala.collection.mutable.ArrayBuffer

object Main {

  def move(m:Int, d1:Int, d2:Int) = {
    val x = m/d2 % 10
    m-9*d1-x*d2+9*d2+x*d1
  }
  def end(m:Int) = m == 123456789

  def main(args:Array[String]) = {
    val hist = new ArrayBuffer[Int]
    val phist = new ArrayBuffer[Int]
    val map = new HashMap[Int,Int]
    var pointer = 0
    var found = false
    var count = 0

    val s = { for(i<-1 to 3) yield readLine.split(" ").reduce(_+_) }.reduce(_+_)
    val idx = 8-s.indexWhere(_=='0')
    phist += idx

    val n = s.map{ c => if(c=='0') '9' else c }.toInt
    hist += n
    map += (n->1)

    if(end(n)) found = true
    while(!found) {
      val len = hist.length
      for(i<-pointer until len if !found) {
        val pos = phist(i)
        val data = hist(i)
        val p = Math.pow(10,pos).toInt
        if((8-pos)%3 != 2) {
          val r = move(data, p, p/10)
          if(end(r)) found = true
          else if(!map.contains(r)) {
            hist += r
            map += (r->1)
            phist += pos-1
          }
        }
        if(!found && (8-pos) % 3 != 0) {
          val r = move(data, p, p*10)
          if(end(r)) found = true
          else if(!map.contains(r)) {
            hist += r
            map += (r->1)
            phist += pos+1
          }
        }
        if(!found && (8-pos) / 3 != 0) {
          val r = move(data, p, p*1000)
          if(end(r)) found = true
          else if(!map.contains(r)) {
            hist += r
            map += (r->1)
            phist += pos+3
          }
        }
        if(!found && (8-pos) / 3 != 2) {
          val r = move(data, p, p/1000)
          if(end(r)) found = true
          else if(!map.contains(r)) {
            hist += r
            map += (r->1)
            phist += pos-3
          }
        }
      }
      pointer = (len-pointer)
      count += 1
    }
    println(count)
  }

}