# Enter your code here. Read input from STDIN. Print output to STDOUT a = map(int,raw_input().split()) b = map(int,raw_input().split()) c = map(int,raw_input().split()) s = [a,b,c] n = 3 #Construct Magic Sqares s1 = [[8, 1, 6], [3, 5, 7], [4, 9, 2]] s2 = [[row[i] for row in s1] for i in range(n)] s3 = zip(*s1[::-1]) s4 = [[row[i] for row in s3] for i in range(n)] s5 = zip(*s3[::-1]) s6 = [[row[i] for row in s5] for i in range(n)] s7 = zip(*s5[::-1]) s8 = [[row[i] for row in s7] for i in range(n)] x1 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s1[i]), xrange(n)) x2 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s2[i]), xrange(n)) x3 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s3[i]), xrange(n)) x4 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s4[i]), xrange(n)) x5 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s5[i]), xrange(n)) x6 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s6[i]), xrange(n)) x7 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s7[i]), xrange(n)) x8 = map(lambda i: map(lambda x, y: abs(x - y), s[i], s8[i]), xrange(n)) z1 = reduce(lambda x, y: x + sum(y), x1, 0) z2 = reduce(lambda x, y: x + sum(y), x2, 0) z3 = reduce(lambda x, y: x + sum(y), x3, 0) z4 = reduce(lambda x, y: x + sum(y), x4, 0) z5 = reduce(lambda x, y: x + sum(y), x5, 0) z6 = reduce(lambda x, y: x + sum(y), x6, 0) z7 = reduce(lambda x, y: x + sum(y), x7, 0) z8 = reduce(lambda x, y: x + sum(y), x8, 0) cost = min(z1, z2, z3, z4, z5, z6, z7, z8) print cost