import java.io.*; import java.util.*; public class Solution { public static void main(String[] args) throws IOException { InputReader in = new InputReader (System.in); int [][][]MAGIC_SQUARES = { {{8,1,6},{3,5,7},{4,9,2}}, {{4,3,8},{9,5,1},{2,7,6}}, {{2,9,4},{7,5,3},{6,1,8}}, {{6,7,2},{1,5,9},{8,3,4}}, {{6,1,8},{7,5,3},{2,9,4}}, {{8,3,4},{1,5,9},{6,7,2}}, {{4,9,2},{3,5,7},{8,1,6}}, {{2,7,6},{9,5,1},{4,3,8}}, }; int [][]arr = new int [3][3]; for (int i=0; i<3; i++) { for (int j=0; j<3; j++) { arr[i][j] = in.readInt (); } } int cost[] = new int [8]; for (int i=0; i<8; i++) { for (int j=0; j<3; j++) { for (int k=0; k<3; k++) { if (MAGIC_SQUARES [i][j][k] != arr [j][k]) { cost [i] += Math.abs (MAGIC_SQUARES [i][j][k] - arr [j][k]); } } } } int min = cost[0]; for (int i=1; i<8; i++) { if (cost[i] < min) { min = cost [i]; } } System.out.println (min); } static final class InputReader { private final InputStream stream; private final byte[] buf = new byte[1024]; private int curChar; private int numChars; public InputReader(InputStream stream) { this.stream = stream; } private int read() throws IOException { if (curChar >= numChars) { curChar = 0; numChars = stream.read(buf); if (numChars <= 0) { return -1; } } return buf[curChar++]; } public final int readInt() throws IOException { return (int)readLong(); } public final long readLong() throws IOException { int c = read(); while (isSpaceChar(c)) { c = read(); if (c == -1) throw new IOException(); } boolean negative = false; if (c == '-') { negative = true; c = read(); } long res = 0; do { res *= 10; res += c - '0'; c = read(); } while (!isSpaceChar(c)); return negative ? -res : res; } public final int[] readIntArray(int size) throws IOException { int[] array = new int[size]; for (int i=0; i