#include #include #include using namespace std; int main() { int a1[3][3] = {{8,1,6},{3,5,7},{4,9,2}}; int a2[3][3] = {{6,1,8},{7,5,3},{2,9,4}}; int a3[3][3] = {{4,9,2},{3,5,7},{8,1,6}}; int a4[3][3] = {{2,9,4},{7,5,3},{6,1,8}}; int a5[3][3] = {{8,3,4},{1,5,9},{6,7,2}}; int a6[3][3] = {{4,3,8},{9,5,1},{2,7,6}}; int a7[3][3] = {{6,7,2},{1,5,9},{8,3,4}}; int a8[3][3] = {{2,7,6},{9,5,1},{4,3,8}}; int arr[3][3]; int minSum[8] = {0}; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { cin >> arr[i][j]; if(arr[i][j] != a1[i][j]) minSum[0] += abs(a1[i][j] - arr[i][j]); if(arr[i][j] != a2[i][j]) minSum[1] += abs(a2[i][j] - arr[i][j]); if(arr[i][j] != a3[i][j]) minSum[2] += abs(a3[i][j] - arr[i][j]); if(arr[i][j] != a4[i][j]) minSum[3] += abs(a4[i][j] - arr[i][j]); if(arr[i][j] != a5[i][j]) minSum[4] += abs(a5[i][j] - arr[i][j]); if(arr[i][j] != a6[i][j]) minSum[5] += abs(a6[i][j] - arr[i][j]); if(arr[i][j] != a7[i][j]) minSum[6] += abs(a7[i][j] - arr[i][j]); if(arr[i][j] != a8[i][j]) minSum[7] += abs(a8[i][j] - arr[i][j]); } } sort(minSum, minSum+8); cout << minSum[0] << endl; return 0; }