sq=[] sq.append(map(int,raw_input().split())) sq.append(map(int,raw_input().split())) sq.append(map(int,raw_input().split())) a = [] a.append([[8,1,6],[3,5,7],[4,9,2]]) a.append([[4,9,2],[3,5,7],[8,1,6]]) a.append([[8,3,4],[1,5,9],[6,7,2]]) a.append([[4,3,8],[9,5,1],[2,7,6]]) a.append([[2,9,4],[7,5,3],[6,1,8]]) a.append([[2,7,6],[9,5,1],[4,3,8]]) a.append([[6,7,2],[1,5,9],[8,3,4]]) a.append([[6,1,8],[7,5,3],[2,9,4]]) m = 8*9 for i in xrange(8): ans = 0 for j in xrange(3): for k in xrange(3): ans+=abs(a[i][j][k] - sq[j][k]) if ans < m: m = ans print m