allPossMagicSquares = [[2, 7, 6, 9, 5, 1, 4, 3, 8], [2, 9, 4, 7, 5, 3, 6, 1, 8], [6, 1, 8, 7, 5, 3, 2, 9, 4], [6, 7, 2, 1, 5, 9, 8, 3, 4], [8, 3, 4, 1, 5, 9, 6, 7, 2], [8, 1, 6, 3, 5, 7, 4, 9, 2], [4, 9, 2, 3, 5, 7, 8, 1, 6], [4, 3, 8, 9, 5, 1, 2, 7, 6]] def computeCost(squareEntries): """ Computes the lowest cost of turning the list squareEntries into one of the lists in allPossMagicSquares. """ allCosts = [] for magicSquare in allPossMagicSquares: cost = 0 for i in xrange(len(squareEntries)): cost += abs(magicSquare[i] - squareEntries[i]) allCosts.append(cost) return min(allCosts) row1 = map(int, raw_input().strip().split(' ')) row2 = map(int, raw_input().strip().split(' ')) row3 = map(int, raw_input().strip().split(' ')) square = row1 + row2 + row3 print computeCost(square)