import java.io.*; import java.util.*; import java.text.*; import java.math.*; import java.util.regex.*; public class Solution { public static void main(String[] args) { /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */ Scanner in = new Scanner(System.in); int[][] n = new int[3][3]; n[0][0]=in.nextInt(); n[0][1]=in.nextInt(); n[0][2]=in.nextInt(); n[1][0]=in.nextInt(); n[1][1]=in.nextInt(); n[1][2]=in.nextInt(); n[2][0]=in.nextInt(); n[2][1]=in.nextInt(); n[2][2]=in.nextInt(); int cost=check1(n); cost = Math.min(cost,check2(n)); cost = Math.min(cost,check3(n)); cost = Math.min(cost,check4(n)); cost = Math.min(cost,check5(n)); cost = Math.min(cost,check6(n)); cost = Math.min(cost,check7(n)); cost = Math.min(cost,check8(n)); System.out.println(cost); } public static int check1(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-4)); cost+=Math.abs((n[0][1]-9)); cost+=Math.abs((n[0][2]-2)); cost+=Math.abs((n[1][0]-3)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-7)); cost+=Math.abs((n[2][0]-8)); cost+=Math.abs((n[2][1]-1)); cost+=Math.abs((n[2][2]-6)); return cost; } public static int check2(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-2)); cost+=Math.abs((n[0][1]-7)); cost+=Math.abs((n[0][2]-6)); cost+=Math.abs((n[1][0]-9)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-1)); cost+=Math.abs((n[2][0]-4)); cost+=Math.abs((n[2][1]-3)); cost+=Math.abs((n[2][2]-8)); return cost; } public static int check3(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-6)); cost+=Math.abs((n[0][1]-1)); cost+=Math.abs((n[0][2]-8)); cost+=Math.abs((n[1][0]-7)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-3)); cost+=Math.abs((n[2][0]-2)); cost+=Math.abs((n[2][1]-9)); cost+=Math.abs((n[2][2]-4)); return cost; } public static int check4(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-8)); cost+=Math.abs((n[0][1]-3)); cost+=Math.abs((n[0][2]-4)); cost+=Math.abs((n[1][0]-1)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-9)); cost+=Math.abs((n[2][0]-6)); cost+=Math.abs((n[2][1]-7)); cost+=Math.abs((n[2][2]-2)); return cost; } public static int check5(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-4)); cost+=Math.abs((n[0][1]-3)); cost+=Math.abs((n[0][2]-8)); cost+=Math.abs((n[1][0]-9)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-1)); cost+=Math.abs((n[2][0]-2)); cost+=Math.abs((n[2][1]-7)); cost+=Math.abs((n[2][2]-6)); return cost; } public static int check6(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-8)); cost+=Math.abs((n[0][1]-1)); cost+=Math.abs((n[0][2]-6)); cost+=Math.abs((n[1][0]-3)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-7)); cost+=Math.abs((n[2][0]-4)); cost+=Math.abs((n[2][1]-9)); cost+=Math.abs((n[2][2]-2)); return cost; } public static int check7(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-6)); cost+=Math.abs((n[0][1]-7)); cost+=Math.abs((n[0][2]-2)); cost+=Math.abs((n[1][0]-1)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-9)); cost+=Math.abs((n[2][0]-8)); cost+=Math.abs((n[2][1]-3)); cost+=Math.abs((n[2][2]-4)); return cost; } public static int check8(int[][] n){ int cost=0; cost+=Math.abs((n[0][0]-2)); cost+=Math.abs((n[0][1]-9)); cost+=Math.abs((n[0][2]-4)); cost+=Math.abs((n[1][0]-7)); cost+=Math.abs((n[1][1]-5)); cost+=Math.abs((n[1][2]-3)); cost+=Math.abs((n[2][0]-6)); cost+=Math.abs((n[2][1]-1)); cost+=Math.abs((n[2][2]-8)); return cost; } }