#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops") #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native") #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; #define REP(i,m,n) for(int i=(int)(m) ; i < (int) (n) ; ++i ) #define rep(i,n) REP(i,0,n) using ll = long long; const int inf=1e9+7; const ll longinf=1LL<<60 ; const ll mod=1e9+7 ; struct UnionFind{ vector par; UnionFind(int n):par(n,-1){} int find(int x){ if(par[x]<0)return x; return par[x]=find(par[x]); } bool unite(int x,int y){ x=find(x); y=find(y); if(x==y)return false; if(par[x]>par[y]){ par[y]+=par[x]; par[x]=y; } else{ par[x]+=par[y]; par[y]=x; } return true; } bool same(int x,int y){ return find(x)==find(y); } int size(int x){ return -par[find(x)]; } }; int main(){ ll a[5][5]; rep(i,5)rep(j,5)cin>>a[i][j]; ll ans = longinf; int b[5][5]; rep(mask,1<<24){ if(mask==0)continue; ll sum = a[4][4]; b[4][4]=0; rep(j,5)rep(k,5){ if(j==4&&k==4)break; b[j][k]=(mask>>(5*j+k))&1; if(b[j][k])sum-=a[j][k]; else sum+=a[j][k]; } UnionFind uf(25); rep(i,5)rep(j,4){ if(b[i][j]==b[i][j+1])uf.unite(5*i+j,5*i+j+1); } rep(i,4)rep(j,5){ if(b[i][j]==b[i+1][j])uf.unite(5*i+j,5*i+j+5); } int idx = 0; while(b[idx/5][idx%5]==0)++idx; if(uf.size(idx)+uf.size(24)==25){ ans = min(ans,abs(sum)); } } cout<