#include #include #include #include #include #include #define Max(a, b) ((a) > (b) ? (a) : (b)) #define Min(a, b) ((a) > (b) ? (b) : (a)) #define abs(x) ((x) > 0 ? (x) : -(x)) #define MOD (ll)1000000007 //10^9 + 7 #define endl printf("\n") typedef long long ll; #define MAX_N (1 << 17) int d[5][5]; ll c[5][5]; ll sum, ans = LLONG_MAX; void dfs(int i, ll r, bool sign) { if (i == 25 || r >= sum / 2 + 1) { ans = Min(ans, abs(sum - ((ll)2) * r)); return; } int s = i / 5, t = i % 5; if (!sign) { d[s][t] = 1; dfs(i + 1, r + c[s][t], true); d[s][t] = 0; dfs(i + 1, r, false); } else { for (int x = -1; x <= 1; x++) { for (int y = -1; y <= 1; y++) { if (x * y == 0 && 0 <= s + x && s + x < 5 && 0 <= t + y && t + y < 5 && d[s + x][t + y] == 1) { d[s][t] = 1; dfs(i + 1, r + c[s][t], true); d[s][t] = 0; break; } } } dfs(i + 1, r, true); } return; } int main(int argc, char *argv[]) { for (int i = 0; i < 5; i++) { for (int j = 0; j < 5; j++) { scanf("%lld", &c[i][j]); sum += c[i][j]; } } dfs(0, 0, false); printf("%lld\n", ans); return 0; }