//Author: Koppu #include #include int magic_square(int p[][3]) { int i, j; int min; int res[8] = {0}; int sum = 0; int a1[3][3] = {{8,1,6}, {3,5,7}, {4,9,2}}; int a2[3][3] = {{4,3,8}, {9,5,1}, {2,7,6}}; int a3[3][3] = {{2,9,4}, {7,5,3}, {6,1,8}}; int a4[3][3] = {{6,7,2}, {1,5,9}, {8,3,4}}; int a5[3][3] = {{6,1,8}, {7,5,3}, {2,9,4}}; int a6[3][3] = {{8,3,4}, {1,5,9}, {6,7,2}}; int a7[3][3] = {{4,9,2}, {3,5,7}, {8,1,6}}; int a8[3][3] = {{2,7,6}, {9,5,1}, {4,3,8}}; for(i = 0; i < 3; i++){ for(j = 0; j < 3; j++){ res[0] = res[0] + abs(a1[i][j] - p[i][j]); res[1] = res[1] + abs(a2[i][j] - p[i][j]); res[2] = res[2] + abs(a3[i][j] - p[i][j]); res[3] = res[3] + abs(a4[i][j] - p[i][j]); res[4] = res[4] + abs(a5[i][j] - p[i][j]); res[5] = res[5] + abs(a6[i][j] - p[i][j]); res[6] = res[6] + abs(a7[i][j] - p[i][j]); res[7] = res[7] + abs(a8[i][j] - p[i][j]); } } min = res[0]; for(i = 1; i < 8; i++) { if(res[i] < min) min = res[i]; } return min; } int main(void) { int a[3][3]; int i, j; for(i = 0; i < 3; i++) { for(j = 0; j < 3; j++) { scanf("%d", &a[i][j]); } } printf("%d", magic_square(a)); return 0; }