#include // clang-format off using Int = long long; #define REP_(i, a_, b_, a, b, ...) for (Int i = (a), lim##i = (b); i < lim##i; i++) #define REP(i, ...) REP_(i, __VA_ARGS__, __VA_ARGS__, 0, __VA_ARGS__) struct SetupIO { SetupIO() { std::cin.tie(nullptr), std::ios::sync_with_stdio(false), std::cout << std::fixed << std::setprecision(13); } } setup_io; #ifndef _MY_DEBUG #define dump(...) #endif // clang-format on /** * author: knshnb * created: Fri Apr 17 21:39:42 JST 2020 **/ template T make_vec(S x) { return x; } template auto make_vec(size_t n, Ts... ts) { return std::vector(ts...))>(n, make_vec(ts...)); } template inline bool chmin(T& a, const T& b) { if (a <= b) return false; a = b; return true; } template inline bool chmax(T& a, const T& b) { if (a >= b) return false; a = b; return true; } signed main() { Int n; std::cin >> n; std::vector offset(n); auto t = make_vec(n, n, 2, 0); REP(i, n) { REP(j, n) { Int x; std::cin >> x, x--; offset[x] += j; if (i - j - 1 >= 0) t[x][i - j - 1][0]++; if (i + j + 1 < n) t[x][i + j + 1][1]++; } } Int ans = 0; REP(i, n) { REP(_, 2) { for (Int j = n - 2; j >= 0; j--) { t[i][j][0] += t[i][j + 1][0]; } REP(j, n - 1) { t[i][j + 1][1] += t[i][j][1]; } } Int mi = 1e9; REP(j, n) { chmin(mi, offset[i] + t[i][j][0] + t[i][j][1]); } ans += mi; } std::cout << ans << std::endl; }