#include using namespace std; #ifdef LOCAL #include "debug.h" #else #define DEBUG(...) #endif int main() { cin.tie(nullptr); ios::sync_with_stdio(false); int n; cin >> n; vector a(n, vector(n)); for (auto&& v : a) { for (auto&& e : v) { cin >> e; --e; } } vector d(n, vector(n)); for (int r = 0; r < n; ++r) { for (int i = 0; i < n; ++i) { for (int j = 0; j < n; ++j) { d[r][a[i][j]] += max(abs(i - r), j); } } } int res = 0; for (int k = 0; k < n; ++k) { int mn = 1e9; for (int r = 0; r < n; ++r) { mn = min(mn, d[r][k]); } res += mn; } cout << res << '\n'; }