# Enter your code here. Read input from STDIN. Print output to STDOUT a1 = [[8,1,6],[3,5,7],[4,9,2]] a2 = [[6,1,8],[7,5,3],[2,9,4]] a3 = [[4,3,8],[9,5,1],[2,7,6]] a4 = [[2,7,6],[9,5,1],[4,3,8]] a5 = [[2,9,4],[7,5,3],[6,1,8]] a6 = [[4,9,2],[3,5,7],[8,1,6]] a7 = [[6,7,2],[1,5,9],[8,3,4]] a8 = [[8,3,4],[1,5,9],[6,7,2]] a = [] su = [] a.append(map(int,raw_input().split())) a.append(map(int,raw_input().split())) a.append(map(int,raw_input().split())) t = 0 for x in range(3): for y in range(3): t = t + abs(a[x][y]-a1[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a2[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a3[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a4[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a5[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a6[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a7[x][y]) su.append(t) t = 0 for x in range(3): for y in range(3): t += abs(a[x][y]-a8[x][y]) su.append(t) print min(su)