# Enter your code here. Read input from STDIN. Print output to STDOUT list_of_squares=[[6, 1, 8, 7, 5, 3, 2, 9, 4], [8, 1, 6, 3, 5, 7, 4, 9, 2], [8, 3, 4, 1, 5, 9, 6, 7, 2], [6, 7, 2, 1, 5, 9, 8, 3, 4], [2, 7, 6, 9, 5, 1, 4, 3, 8], [4, 3, 8, 9, 5, 1, 2, 7, 6], [4, 9, 2, 3, 5, 7, 8, 1, 6], [2, 9, 4, 7, 5, 3, 6, 1, 8]] l=raw_input().split() l.extend(raw_input().split()) l.extend(raw_input().split()) l = [ int(x) for x in l ] minsum=999999 for i in list_of_squares: summ=0 for j in xrange(0,len(i)): summ+=abs((l[j]-i[j])) if summ < minsum: minsum=summ print minsum