object Main {
    def main(args: Array[String]): Unit = {
        val in = io.StdIn.readLine.split(" ").map(_.toInt)
        ((1 to 9) diff in) match {
            case l if (l.isEmpty) => println(0)
            case l => {
                val d = l.permutations.map(p => {
                    p.foldLeft(in)((arr, n) => arr.updated(arr.indexOf(-1), n))
                }).count(n => n(0)+n(1)*10+n(2)+n(3)*100+n(4)*10+n(5) == n(6)*100+n(7)*10+n(8))
                if (d == 0) println(-1)
                else println(d)
            }
        }
    }
}