# Enter your code here. Read input from STDIN. Print output to STDOUT import itertools x=[1,2,3,4,5,6,7,8,9] l=list(itertools.permutations(x,9)) s=[] for a in l: if (a[0]+a[3]+a[6])==15 and (a[1]+a[4]+a[7])==15 and (a[2]+a[5]+a[8])==15: if (a[0]+a[1]+a[2])==15 and (a[6]+a[7]+a[8])==15: if (a[0]+a[4]+a[8])==15 and a[2]+a[4]+a[6]==15: s.append(a) t=[] r=[] c=[] for _ in range(0,3): t.append(map(int,raw_input().split(' '))) for val in t: for v in val: r.append(v) for i in range(0,len(s)): cost=0 for j in range(0,len(r)): if r[j]!=s[i][j]: cost+=abs(r[j]-s[i][j]) c.append(cost) print min(c)