#C3786. Magic Square Transformation

    ID: 47251 Type: Default 1000ms 256MiB

Magic Square Transformation

Magic Square Transformation

You are given a 3×3 grid of integers. Your task is to convert this grid into a magic square with minimum cost. A magic square is a 3×3 grid in which every row, column, and both main diagonals all add up to the same constant. The cost of changing a number is defined as the absolute difference between the original and the new value. In other words, if you change an element from si to mi, then the cost incurred is |si - mi|. The overall cost is the sum of the individual costs, i.e.,

\[ \text{cost} = \sum_{i=0}^{8} |s_i - m_i| \]

Find the minimum cost required to convert the given grid into any magic square. There are only eight possible 3×3 magic squares, which can be precomputed and used to solve the problem.

inputFormat

The input consists of 3 lines. Each line contains 3 space-separated integers representing a row of the grid.

outputFormat

Output a single integer, which is the minimum cost to convert the input grid into a magic square.

## sample
4 9 2
3 5 7
8 1 6
0