import java.io.*; import java.util.*; class MyScanner { BufferedReader br; StringTokenizer st; public MyScanner() { br = new BufferedReader(new InputStreamReader(System.in)); } String next() { while (st == null || !st.hasMoreElements()) { try { st = new StringTokenizer(br.readLine()); } catch (IOException e) { e.printStackTrace(); } } return st.nextToken(); } int nextInt() { return Integer.parseInt(next()); } long nextLong() { return Long.parseLong(next()); } double nextDouble() { return Double.parseDouble(next()); } String nextLine(){ String str = ""; try { str = br.readLine(); } catch (IOException e) { e.printStackTrace(); } return str; } } public class Solution { public static void main(String[] args) { MyScanner input = new MyScanner(); int matrix[][] = new int[3][3]; for(int i = 0; i < 3; i++) for(int j = 0; j < 3; j++) matrix[i][j] = input.nextInt(); int magic[][][] = {{{4,9,2},{3,5,7},{8,1,6}},{{8,3,4},{1,5,9},{6,7,2}} ,{{6,1,8},{7,5,3},{2,9,4}},{{2,7,6},{9,5,1},{4,3,8}} ,{{2,9,4},{7,5,3},{6,1,8}},{{4,3,8},{9,5,1},{2,7,6}} ,{{8,1,6},{3,5,7},{4,9,2}},{{6,7,2},{1,5,9},{8,3,4}}}; int min = 999; for(int i = 0; i < 8; i++){ int count = 0; for(int j = 0; j < 3; j ++) for(int k = 0; k < 3; k++) count += Math.abs(magic[i][j][k] - matrix[j][k]); if(count < min) min = count; } System.out.println(min); } }