#include using namespace std; int main() { ios::sync_with_stdio(false); cin.tie(0); int n; cin >> n; vector> A(n, vector(n)), B(n, vector(n)); vector s(n), r(n), add(n); for(int y = 0; y < n; y++){ for(int x = 0; x < n; x++){ cin >> A[y][x]; s[--A[y][x]] += x; r[x] += add[x]; B[x][y] = r[x]; } for(int x = 0; x <= y; x++) add[A[y - x][x]]++; } fill(r.begin(), r.end(), 0); fill(add.begin(), add.end(), 0); reverse(A.begin(), A.end()); for(int y = 0; y < n; y++){ for(int x = 0; x < n; x++){ r[x] += add[x]; B[x][n - 1 - y] += r[x] + s[x]; } for(int x = 0; x <= y; x++) add[A[y - x][x]]++; } int ans = 0; for(int i = 0; i < n; i++) ans += *min_element(B[i].begin(), B[i].end()); cout << ans << '\n'; }