#include #include using namespace std; using namespace atcoder; //using mint = modint1000000007; //const int mod = 1000000007; //using mint = modint998244353; //const int mod = 998244353; //const int INF = 1e9; const long long LINF = 1e18; #define rep(i, n) for (int i = 0; i < (n); ++i) #define rep2(i,l,r)for(int i=(l);i<(r);++i) #define rrep(i, n) for (int i = (n-1); i >= 0; --i) #define rrep2(i,l,r)for(int i=(r-1);i>=(l);--i) #define all(x) (x).begin(),(x).end() #define allR(x) (x).rbegin(),(x).rend() #define endl "\n" #define P pair template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } int index(int x, int y) { return x * 5 + y; } int main() { ios::sync_with_stdio(false); cin.tie(nullptr); vector a(5, vector(5)); rep(i, 5)rep(j, 5)cin >> a[i][j]; long long ans = LINF; vectorv0 = { 0,1,2,3,4,9,14,19,24,23,22,21,20,15,10,5 }; vectorv1 = { 6,7,8,11,12,13,16,17,18 }; rep(i, v0.size())rep2(j, i + 1, v0.size()) { vectorc(25); rep2(k, i, j + 1) c[v0[k]] = 1; rep(k, 1 << 9) { auto cc = c; rep(l, 9) { if (1 & (k >> l))cc[v1[l]] = 1; } dsu uf(25); rep(s, 5) rep(t, 5) { if (s != 4) { int ns = s + 1; int nt = t + 0; int i0 = index(s, t); int i1 = index(ns, nt); if (cc[i0] == cc[i1])uf.merge(i0, i1); } if (t != 4) { int ns = s + 0; int nt = t + 1; int i0 = index(s, t); int i1 = index(ns, nt); if (cc[i0] == cc[i1])uf.merge(i0, i1); } } auto v = uf.groups(); if (2 != v.size()) continue; long long val = 0; rep(l, 25) { if (cc[l])val += a[l / 5][l % 5]; else val -= a[l / 5][l % 5]; } chmin(ans, abs(val)); } } cout << ans << endl; return 0; }