square = [[int(i) for i in input().split()] for i in range(3)] square1 = [[4,9,2],[3,5,7],[8,1,6]] square2 = [[2,9,4],[7,5,3],[6,1,8]] square3 = [[8,3,4],[1,5,9],[6,7,2]] square4 = [[6,1,8],[7,5,3],[2,9,4]] square5 = [[2,7,6],[9,5,1],[4,3,8]] square6 = [[4,3,8],[9,5,1],[2,7,6]] square7 = [[8,1,6],[3,5,7],[4,9,2]] square8 = [[6,7,2],[1,5,9],[8,3,4]] squares = [square1,square2,square3,square4,square5,square6,square7,square8] mindiff = 1000 for i in squares: diff = 0 for j in range(3): for k in range(3): diff += abs(square[j][k] - i[j][k]) if mindiff > diff: mindiff = diff print(mindiff)