#include #include #include #define N_PATTERN 8 #define PATTERN_SIZE 9 const int pattern[8][9] = {{8, 1, 6, 3, 5, 7, 4, 9, 2}, {6, 1, 8, 7, 5, 3, 2, 9, 4}, {4, 9, 2, 3, 5, 7, 8, 1, 6}, {2, 9, 4, 7, 5, 3, 6, 1, 8}, {8, 3, 4, 1, 5, 9, 6, 7, 2}, {4, 3, 8, 9, 5, 1, 2, 7, 6}, {6, 7, 2, 1, 5, 9, 8, 3, 4}, {2, 7, 6, 9, 5, 1, 4, 3, 8}}; #define min(a,b) ((a) < (b) ? (a) : (b)) int get_min_diff(int * check) { int ret = 10000; for(int i = 0; i < N_PATTERN; ++i) { int curr_diff = 0; for(int j = 0; j < PATTERN_SIZE; ++j) curr_diff += abs(check[j] - pattern[i][j]); ret = min(curr_diff, ret); } return ret; } #undef min int main() { int check[9]; for(int i = 0; i < PATTERN_SIZE; ++i) scanf("%d", &check[i]); printf("%d", get_min_diff(check)); return 0; }