#include #include #include #include #include using namespace std; typedef vector vi; typedef vector vvi; vi m1 = {8,1,6,3,5,7,4,9,2}; vi m2 = {4,3,8,9,5,1,2,7,6}; vi m3 = {2,9,4,7,5,3,6,1,8}; vi m4 = {6,7,2,1,5,9,8,3,4}; vi m5 = {4,9,2,3,5,7,8,1,6}; vi m6 = {2,7,6,9,5,1,4,3,8}; vi m7 = {6,1,8,7,5,3,2,9,4}; vi m8 = {8,3,4,1,5,9,6,7,2}; vvi magic; vi input = {0,0,0,0,0,0,0,0,0}; int main() { magic.push_back(m1); magic.push_back(m2); magic.push_back(m3); magic.push_back(m4); magic.push_back(m5); magic.push_back(m6); magic.push_back(m7); magic.push_back(m8); for (int i = 0; i < 9; i++) { cin >> input[i]; } int cost, tmp; cost = 100000; tmp = 0; for (int i = 0; i < 8; i++) { tmp = 0; for (int j = 0; j < 9; j++) { tmp += abs(magic[i][j] - input[j]); } cost = min(tmp, cost); } printf("%d", cost); return 0; }