#include using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define REP(i,n) FOR(i,0,n) #define ALL(v) begin(v),end(v) template inline bool chmax(A & a, const B & b) { if (a < b) { a = b; return true; } return false; } template inline bool chmin(A & a, const B & b) { if (a > b) { a = b; return true; } return false; } using ll = long long; using pii = pair; constexpr ll INF = 1ll<<30; constexpr ll longINF = 1ll<<60; constexpr ll MOD = 1000000007; constexpr bool debug = false; //---------------------------------// int main() { int N; cin >> N; vector A(N, vector(N)); vector> ev(N); vector sum(N); REP(i, N) REP(j, N) { scanf("%d", &A[i][j]); --A[i][j]; ev[max(i - j, 0)].emplace_back(A[i][j]); ev[min(i + j, N - 1)].emplace_back(A[i][j]); sum[A[i][j]] += max(i, j); } vector dif(N, -N), mn(N, INF); REP(i, N) { REP(j, N) chmin(mn[j], sum[j]); for (auto a : ev[i]) ++dif[a]; REP(j, N) sum[j] += dif[j]; } cout << accumulate(ALL(mn), 0) << endl; }