# Enter your code here. Read input from STDIN. Print output to STDOUT square=list(raw_input().strip().split(' ')) square2=list(raw_input().strip().split(' ')) square3=list(raw_input().strip().split(' ')) for char in square2: square.append(char) for char in square3: square.append(char) square=[int(char) for char in square] #All 3x3 magic squares: a=[8,1,6,3,5,7,4,9,2] b=[4,3,8,9,5,1,2,7,6] c=[2,9,4,7,5,3,6,1,8] d=[6,7,2,1,5,9,8,3,4] e=[6,1,8,7,5,3,2,9,4] f=[8,3,4,1,5,9,6,7,2] g=[4,9,2,3,5,7,8,1,6] h=[2,7,6,9,5,1,4,3,8] a_diff=[abs(i-j) for i,j in zip(square,a)] b_diff=[abs(i-j) for i,j in zip(square,b)] c_diff=[abs(i-j) for i,j in zip(square,c)] d_diff=[abs(i-j) for i,j in zip(square,d)] e_diff=[abs(i-j) for i,j in zip(square,e)] f_diff=[abs(i-j) for i,j in zip(square,f)] g_diff=[abs(i-j) for i,j in zip(square,g)] h_diff=[abs(i-j) for i,j in zip(square,h)] difflist=[sum(a_diff),sum(b_diff),sum(c_diff),sum(d_diff),sum(e_diff),sum(f_diff),sum(g_diff),sum(h_diff)] print min(difflist)