object Solution { def main(args: Array[String]) { val sc = new java.util.Scanner(System.in) val target1 = Array[Array[Int]](Array[Int](4, 9, 2), Array[Int](3, 5, 7), Array[Int](8, 1, 6)) val target2 = Array[Array[Int]](Array[Int](8, 3, 4), Array[Int](1, 5, 9), Array[Int](6, 7, 2)) val target3 = Array[Array[Int]](Array[Int](6, 1, 8), Array[Int](7, 5, 3), Array[Int](2, 9, 4)) val target4 = Array[Array[Int]](Array[Int](2, 7, 6), Array[Int](9, 5, 1), Array[Int](4, 3, 8)) val target5 = Array[Array[Int]](Array[Int](2, 9, 4), Array[Int](7,5,3), Array[Int](6,1,8)) val target6 = Array[Array[Int]](Array[Int](4, 3, 8), Array[Int](9,5,1), Array[Int](2,7,6)) val target7 = Array[Array[Int]](Array[Int](8, 1, 6), Array[Int](3,5,7), Array[Int](4,9,2)) val target8 = Array[Array[Int]](Array[Int](6, 7, 2), Array[Int](1,5,9), Array[Int](8,3,4)) val m = new Array[Array[Int]](3) m(0) = new Array[Int](3); m(1) = new Array[Int](3); m(2) = new Array[Int](3) m(0)(0) = sc.nextInt(); m(0)(1) = sc.nextInt(); m(0)(2) = sc.nextInt(); m(1)(0) = sc.nextInt(); m(1)(1) = sc.nextInt(); m(1)(2) = sc.nextInt(); m(2)(0) = sc.nextInt(); m(2)(1) = sc.nextInt(); m(2)(2) = sc.nextInt() def delta(source: Array[Array[Int]], target: Array[Array[Int]]): Int = { Math.abs(source(0)(0) - target(0)(0)) + Math.abs(source(0)(1) - target(0)(1)) + Math.abs(source(0)(2) - target(0)(2)) + Math.abs(source(1)(0) - target(1)(0)) + Math.abs(source(1)(1) - target(1)(1)) + Math.abs(source(1)(2) - target(1)(2)) + Math.abs(source(2)(0) - target(2)(0)) + Math.abs(source(2)(1) - target(2)(1)) + Math.abs(source(2)(2) - target(2)(2)) } println(Seq( delta(m, target1), delta(m, target2), delta(m, target3), delta(m, target4), delta(m, target5), delta(m, target6), delta(m, target7), delta(m, target8) ).min) } }