#include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define rep(i, n) for (ll i = 0; i < (ll)n; i++) #define repf(i, a, b) for (ll i = (ll)a; i < (ll)b; i++) #define repr(i, a, b) for (ll i = (ll)a; i > (ll)b; i--) #define all(v) (v).begin(), (v).end() #define mp(a, b) make_pair(a, b) #define pb(x) push_back(x) #define eb(x) emplace_back(x) #define F first #define S second typedef long long ll; typedef long double ld; typedef pair pii; typedef pair pll; typedef pair pdd; typedef vector vll; typedef vector vvll; typedef vector vii; typedef vector vvii; const ll mod = 1e9 + 7; const int infi = 1147483600; const ll infl = 1e17; const char ENDL = '\n'; struct UnionFind { vector root; int n; void __init__(int sz) { n = sz; root.resize(n, -1); } int find(int x) { if (root[x] >= 0) { root[x] = find(root[x]); return root[x]; } return x; } void unit(int a, int b) { int ra, rb; ra = find(a); rb = find(b); if (ra == rb) { return; } else if (root[ra] <= root[rb]) { root[ra] += root[rb]; root[rb] = ra; } else { root[rb] += root[ra]; root[ra] = rb; } return; } int count() { set cnt; rep(i, n) cnt.insert(find(i)); return cnt.size(); } }; int main() { vll MAP(25); rep(i, 25) cin >> MAP[i]; vii move = {5, -5, 1, -1}; map mawari; rep(i, 5) mawari[i] = i; repf(i, 1, 5) mawari[i + 4] = 5 * i + 4; repr(i, 1, 5) mawari[i + 8] = 24-i; mawari[13] = 15; mawari[14] = 10; mawari[15] = 5; map naka; repf(i,1,4){ repf(j,1,4){ naka[3 * (i - 1) + j - 1] = 5 * i + j; } } ll ans = infl; rep(i,16){ repf(j,i,i+17){ vii get(25, 0); rep(k,16){ if (i<=k>k)&1){ get[naka[k]] = 1; } } UnionFind uf; uf.__init__(25); ll now = 0; rep(ii,25){ if (get[ii]) now += MAP[ii]; else now -= MAP[ii]; rep(m,4){ if (move[m]+ii>=0 && move[m]+ii<25){ if (get[move[m]+ii]==get[ii]){ uf.unit(move[m] + ii, ii); } } } } if (uf.count()==2){ ans = min(ans, abs(now)); } } } } cout << ans << ENDL; }