#ifdef DEBUG
#include "utils.cpp"
#endif
#include <bits/stdc++.h>


using namespace std;
using ll = int64_t;
#define int int64_t

 
/* ==== declarations ====== */
// functions
void solve();
void getInput();
 
// global variables
int N;
vector<vector<int>> C;
vector<vector<bool>> O;
vector<pair<int, int>> operations;

/* ======================== */
void getInput() {
    cin>>N;
    operations = vector<pair<int, int>>(N*N);
    for(int i=0; i<N*N; ++i) {
        int num;
        cin>>num;
        // operations[num-1] = {i/N, i%N};
        num--;
        operations[i] = {num/N, num%N};
    }

    O = vector<vector<bool>>(N, vector<bool>(N, true));
    C = vector<vector<int>>(N, vector<int>(N, 0));


    for(int i=0; i<N; ++i) {
        for (int j=0; j<N; ++j) {
            int a = min(i, N-1-i);
            int b = min(j, N-1-j);
            C[i][j] = min(a, b);
        }
    }
}
 
void updateNeighbors(pair<int, int> pos) {
    vector<int> ret;
    queue<pair<int, int>> que;
    que.push(pos);
    int cost = C[pos.first][pos.second];
    // print("update: ", pos, "cost: ", cost);
    
    vector<vector<bool>> visit(N, vector<bool>(N, false));
    while(!que.empty()) {
        auto op = que.front();
        que.pop();
        if(visit[op.first][op.second]) continue;
        
        visit[op.first][op.second] = true;
        vector<int> dx = {1, -1, 0, 0};
        vector<int> dy = {0, 0, 1, -1};
        
        for (int i=0; i<4; ++i) {
            int x = op.first + dx[i];
            int y = op.second + dy[i];
            if (x<0 or y<0 or x>=N or y>=N) continue;

            
            
            if (O[x][y]) {
                C[x][y] = min(C[x][y], cost);
            } else {
                que.push({x, y});
            }
        }
        
    }
    
}
 
void solve() {
    int ans;
    for(auto op: operations) {
        ans += C[op.first][op.second];
        O[op.first][op.second] = false;


        // print("====================================");
        updateNeighbors(op);
        
        // print(op);
        // print(C);
        
        // vector<int> dx = {1, -1, 0, 0};
        // vector<int> dy = {0, 0, 1, -1};
        // for (int i=0; i<4; ++i) {
        //     int x = op.first + dx[i];
        //     int y = op.second + dy[i];
        //     if (x<0 or y<0 or x>=N or y>=N) continue;

        // }

    }
    print(ans);
}
 
 
 
signed main(){
    getInput();
    solve();
    return 0;
}