# Enter your code here. Read input from STDIN. Print output to STDOUT# Enter your code here. Read input from STDIN. Print output to STDOUT4 9 2 r1 = map(int, raw_input().strip().split(' ')) r2 = map(int, raw_input().strip().split(' ')) r3 = map(int, raw_input().strip().split(' ')) r = r1 + r2 + r3 p1 = [2,7,6,9,5,1,4,3,8] p2 = [4,9,2,3,5,7,8,1,6] p3 = [6,1,8,7,5,3,2,9,4] p4 = [8,3,4,1,5,9,6,7,2] p5 = [2,9,4,7,5,3,6,1,8] p6 = [4,3,8,9,5,1,2,7,6] p7 = [6,7,2,1,5,9,8,3,4] p8 = [8,1,6,3,5,7,4,9,2] p = [p1, p2, p3, p4, p5, p6, p7, p8] min_cost = 45 for index, pat in enumerate(p): cost = 0 for index, val in enumerate(pat): if val != r[index]: cost += abs(val - r[index]) min_cost = cost if min_cost > cost else min_cost print min_cost