#include "bits/stdc++.h" #pragma warning(disable:4996) using namespace std; #define int long long #define rep(i,n) for(int i=0;i P; int a[1005][1005]; vector

pos[1005]; signed main() { int n; cin >> n; rep(i, n) { rep(j, n) { cin >> a[i][j]; a[i][j]--; pos[a[i][j]].push_back(P(i, j)); } } int ans = 0; rep(i, n) { vectorimos(n + 1); assert(pos[i].size() == n); rep(j, n) { int x = pos[i][j].first, y = pos[i][j].second; imos[0]--; imos[max(x - y,0ll)]++; imos[min(x + y + 1,n)]++; imos[n]--; } rep(j, n)imos[j + 1] += imos[j]; int pos2 = 0; rep(j, n-1) { if (imos[j] < 0 && imos[j + 1] >= 0) { pos2 = j + 1; break; } } rep(j, n) { int x = pos[i][j].first, y = pos[i][j].second; ans += max(abs(x - pos2), y); } } cout << ans << endl; }