#include #include #include #include using namespace std; typedef pair PII; typedef long long LL; const int N = 10; const LL INF = 9e18; LL ans; queue q; bool st[N][N]; int a[N][N], col[N][N], dx[4] = { 1, -1, 0, 0 }, dy[4] = { 0, 0, 1, -1 }; int Calc(int x, int y) { memset(st, 0, sizeof(st)); int ret = 0; q.push({ x, y }); st[x][y] = true; while (!q.empty()) { auto [xx, yy] = q.front(); q.pop(); ++ret; for (int i = 0; i < 4; ++i) { int nx = xx + dx[i], ny = yy + dy[i]; if (1 <= nx && nx <= 5 && 1 <= ny && ny <= 5 && col[nx][ny] == col[x][y] && !st[nx][ny]) { q.push({ nx, ny }); st[nx][ny] = true; } } } return ret; } int main() { // freopen("cake.in", "r", stdin); // freopen("cake.out", "w", stdout); for (int i = 1; i <= 5; ++i) { for (int j = 1; j <= 5; ++j) { scanf("%d", &a[i][j]); } } ans = INF; for (int mask = 1; mask < (1 << 25) - 1; ++mask) { LL sum1 = 0LL, sum2 = 0LL; int x1, y1, x2, y2; x1 = y1 = x2 = y2 = 0; for (int i = 1; i <= 5; ++i) { for (int j = 1; j <= 5; ++j) { if (mask >> (5 * (i - 1) + j - 1) & 1) { col[i][j] = 1; x1 = i, y1 = j; sum1 += a[i][j]; } else { col[i][j] = 2; sum2 += a[i][j]; x2 = i, y2 = j; } } } int cnt = 0; cnt += Calc(x1, y1); cnt += Calc(x2, y2); if (cnt == 25) { ans = min(ans, llabs(sum1 - sum2)); } } printf("%lld\n", ans); return 0; }