#pragma GCC target("avx2") #pragma GCC optimize("O3") #pragma GCC optimize("unroll-loops") #include //#include using namespace std; //using namespace atcoder; void solve(){ int n; cin >> n; vector> dp(n, vector(n, 0)); int a; for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ cin >> a; a--; for(int k = 0; k < n; k++){ int ii = abs(k - i); if(ii <= j) dp[a][k] += j; else dp[a][k] += ii; } } } int ans = 0; for(int i = 0; i < n; i++){ int mi = n * n; for(int j = 0; j < n; j++){ if(dp[i][j] < mi) mi = dp[i][j]; } ans += mi; } cout << ans << "\n"; } int main(){ cin.tie(0)->sync_with_stdio(0); int t; t = 1; //cin >> t; while(t--) solve(); }