#include #include #include using namespace std; int n, i, j; int main(){ cin >> n; long long a[n][n]; long long tmp[n], ans[n], g[n]; long long answer = 0; for(i = 0; i < n; i++){for(j = 0; j < n; j++){cin >> a[i][j];} g[i] = 0; tmp[i] = 0;} for(i = 0;i < n; i++){ for(j = 0; j < n; j++){ g[a[i][j] - 1] += max(i, j); if(i >= j) tmp[a[i][j] - 1] -= 1; } } for(i = 0; i < n; i++) {ans[i] = g[i];} for(i = 1; i < n; i++){ for(j = 0; j < n; j++){ if(j - i + 1 == 0) tmp[a[i - 1][0] - 1] += 2; else tmp[a[j][abs(j - i + 1)] - 1] += 1; } for(j = 0; j < n; j++){ g[j] += tmp[j]; if(g[j] < ans[j]) ans[j] = g[j]; } } for(i = 0; i < n; i++) answer += ans[i]; cout << answer << endl; return 0; }