#include"bits/stdc++.h" using namespace std; #define int long long #define rep(i,n) for(int i=0;i P; vector

G[1005]; signed main() { cin.tie(0); ios::sync_with_stdio(false); int n; cin >> n; rep(i, n) { rep(j, n) { int a; cin >> a; a--; G[a].push_back(P(i, j)); } } int ans = 0; rep(i, n) { vectorp1(n), p2(n); rep(j, n) { p1[0]++; p1[max(0ll, G[i][j].first - G[i][j].second)]--; p2[min(n - 1, G[i][j].second + G[i][j].first+1)]++; p2[n - 1]--; } rep(j, n - 1) { p1[j + 1] += p1[j]; p2[j + 1] += p2[j]; } rep(j, n - 1) { p2[j + 1] += p2[j]; p1[n - 2 - j] += p1[n - 1 - j]; } int mn = 1e9, id; rep(j, n) { if (mn > p1[j] + p2[j]) { mn = p1[j] + p2[j]; id = j; } } rep(j, n) { ans += max(G[i][j].second, abs(G[i][j].first - id)); } } cout << ans << endl; }