#include #include #include #include #include #include #include #include using namespace std; using ll = long long; int main() { constexpr int N = 5; int c[N][N]; for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { cin >> c[i][j]; } } int p[16]; for (int h = 0; h < 4; h++) { p[h] = h; p[h + 4] = 4 + h * N; p[h + 8] = 24 - h; p[h + 12] = 20 - h * N; } int d[N][N]; int d0; ll s; int x; function f = [&](int i, int j) { if (d[i][j] != d0) return; s += c[i][j]; x++; d[i][j] ^= 4; if (i > 0) f(i - 1, j); if (i < N - 1) f(i + 1, j); if (j > 0) f(i, j - 1); if (j < N - 1) f(i, j + 1); }; ll r = 1LL << 60; for (int k = 0; k < 16; k++) { for (int l = 1; l <= 16; l++) { for (int h = 0; h < 16; h++) { d[0][p[(k + h) % 16]] = h < l; } for (int h = 0; h < 1 << 9; h++) { for (int i = 0; i < 3; i++) { for (int j = 0; j < 3; j++) { d[i + 1][j + 1] = h >> (i * 3 + j) & 1; } } s = 0; x = 0; for (int a = 0; a < 2; a++) { int e = p[k]; if (a == 0) { e = -1; for (int h = 0; h < N * N; h++) { if ((&d[0][0])[h] == 0) { e = h; break; } } if (e < 0) continue; } int i = e / N, j = e % N; d0 = a; f(i, j); s *= -1; } if (x != N * N) continue; r = min(r, abs(s)); for (int i = 0; i < N; i++) { for (int j = 0; j < N; j++) { d[i][j] &= ~4; } } } } } cout << r << endl; return 0; }