# Enter your code here. Read input from STDIN. Print output to STDOUT arr =[ [[4,9,2],[3,5,7],[8,1,6]] ,[[8,1,6],[3,5,7],[4,9,2]] , [[2,9,4],[7,5,3],[6,1,8]] ,[[6,1,8],[7,5,3],[2,9,4]] ] a = [] for _ in range(3): a.append(map(int, raw_input().strip().split(" ")) ) m = float("inf") for test in arr: cost = 0 for i in range(3): for j in range(3): cost += abs(test[i][j]-a[i][j]) m = min(m,cost) test = map(list, zip(*test)) cost = 0 for i in range(3): for j in range(3): cost += abs(test[i][j]-a[i][j]) m = min(m,cost) print m