import java.io.*; import java.util.*; import java.util.function.Supplier; import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; import java.util.stream.IntStream; public class Solution { private static final List>> MAGIC_SQUARES = Arrays.asList( Arrays.asList( Arrays.asList(8, 1, 6), Arrays.asList(3, 5, 7), Arrays.asList(4, 9, 2) ), Arrays.asList( Arrays.asList(6, 1, 8), Arrays.asList(7, 5, 3), Arrays.asList(2, 9, 4) ), Arrays.asList( Arrays.asList(4, 3, 8), Arrays.asList(9, 5, 1), Arrays.asList(2, 7, 6) ), Arrays.asList( Arrays.asList(2, 7, 6), Arrays.asList(9, 5, 1), Arrays.asList(4, 3, 8) ), Arrays.asList( Arrays.asList(2, 9, 4), Arrays.asList(7, 5, 3), Arrays.asList(6, 1, 8) ), Arrays.asList( Arrays.asList(4, 9, 2), Arrays.asList(3, 5, 7), Arrays.asList(8, 1, 6) ), Arrays.asList( Arrays.asList(6, 7, 2), Arrays.asList(1, 5, 9), Arrays.asList(8, 3, 4) ), Arrays.asList( Arrays.asList(8, 3, 4), Arrays.asList(1, 5, 9), Arrays.asList(6, 7, 2) ) ); public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); List> matrix = IntStream.range(0, 3) .boxed() .map(i -> readLine(br)) .map(s -> Arrays.stream(s.split(" ")).map(Integer::valueOf).collect(Collectors.toList())) .collect(Collectors.toList()); System.out.println(MAGIC_SQUARES.stream().mapToInt(ms -> getDiff(ms, matrix)).min().getAsInt()); } private static int getDiff(List> ms, List> matrix) { int diff = 0; for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { diff += Math.abs(ms.get(i).get(j) - matrix.get(i).get(j)); } } return diff; } private static String readLine(BufferedReader br) { try { return br.readLine(); } catch (IOException e) { throw new RuntimeException(e); } } }