# Enter your code here. Read input from STDIN. Print output to STDOUT

M = (raw_input().split(), raw_input().split(), raw_input().split())
M = [[int(x) for x in y] for y in M]

# ROTATION FUNCTIONS
def rotate(M) :
    return[[M[2][0], M[1][0],M[0][0]],[M[2][1], M[1][1],M[0][1]],[M[2][2], M[1][2],M[0][2]]]

def hflip(M) :
    return map(lambda x:x[::-1], M)

def vflip(M) :
    return M[::-1]

# GENERATE ALL TRANSFORMS
def distinct(coll) :
    seen = []
    for x in coll :
        if x not in seen :
            seen = seen + [x]
    return seen

def transforms(M) :
    ts = [M, rotate(M), rotate(rotate(M)), rotate(rotate(rotate(M)))]    
    ret = []
    for t in ts :
        ret += [t, hflip(t), vflip(t), hflip(vflip(t))]
    return ret

def absolute_difference(M, N) :
    return sum(map(lambda (a,b) : abs(a - b), zip(sum(M,[]),sum(N,[]))))

N = [[2,7,6],[9,5,1],[4,3,8]]
print min(*[absolute_difference(M, m) for m in transforms(N)])

    
"""

def spans(M) :
    return map(list,M + zip(*M) + [[M[0][0], M[1][1], M[2][2]]] + [[M[0][2], M[1][1], M[0][2]]])

def remove(coll,x) :
    ret = coll + []
    ret.remove(x)
    return ret
    
def is_magic(M) :
    S = spans(M)
    print S
    return (1 == len(set([sum(xs) for xs in S])))
   
    
# 00 01 02 
# 10 11 12
# 20 21 22

def permutations(coll) :
    if coll == [] :
        return [[]]
    else :
        return [[x] + y for x in coll for y in permutations(remove(coll,x))]
    
def divide(p) :
    return (p[0:3],p[3:6],p[6:10])

for p in permutations(range(1,10))[:100] :
    M = divide(p)
    if is_magic(M) :
        print M
"""