#include<bits/stdc++.h>
#define rep(i,n) for (int i = 0; i < (n); ++i)
using namespace std;
typedef long long ll;
#define INF 1e9
typedef pair<int,int> P;

int main(){
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N;
    cin >> N;
    int N2 = N*N;
    vector<int> Para(N2,0);
    vector<vector<int>> dist(N,vector<int>(N,0));
    vector<vector<int>> o_v(N,vector<int>(N,1));//occupied vacant
/*    int dist[N+1][N+1];
    rep(i,N){
        rep(j,N){
            dist[i][j] = 0;
        }
    }
*/
    rep(i,N2) cin >> Para[i]; 

    int ans = 0;
    rep(i,N){
        rep(j,N){//cost initiation
            int res = min(i,j);
            res = min(res,N-1-i);
            res = min(res,N-1-j);
            dist[i][j] = res;
        }
    }
    rep(i,N2){
        int res2,w,h = 0;
        res2 = Para[i];
        w = (res2-1)%N;
        h = (res2-1)/N;
        queue<P> que;

        ans += dist[w][h];
        o_v[w][h] = 0;

        if((w-1)>= 0  && (dist[w-1][h] > dist[w][h])) {
            dist[w-1][h] = dist[w][h];
            que.push(P(w-1,h));
        }
        if((w+1)<= N-1 && (dist[w+1][h] > dist[w][h])){
            dist[w+1][h] = dist[w][h];
            que.push(P(w+1,h));
        }
        if((h+1)<= N-1 && (dist[w][h+1] > dist[w][h])){
            dist[w][h+1] = dist[w][h];
            que.push(P(w,h+1));
        }
        if((h-1)>= 0 && (dist[w][h-1] > dist[w][h])){
            dist[w][h-1] = dist[w][h];
            que.push(P(w,h-1));
        }
        
        while(!que.empty()){
            int w2 = que.front().first;
            int h2 = que.front().second;
            que.pop();

            if(o_v[w2][h2] == 0){
                if((w2-1)>= 0  && (dist[w2-1][h2] - dist[w2][h2]) > 0) {
                    dist[w2-1][h2] = dist[w2][h2];
                    que.push(P(w2-1,h2));
                }
                if((w2+1)<= N-1  && (dist[w2+1][h2] - dist[w2][h2]) > 0){
                    dist[w2+1][h2] = dist[w2][h2];
                    que.push(P(w2+1,h2));
                }
                if((h2+1)<= N-1 && (dist[w2][h2+1] - dist[w2][h2] >0)){
                    dist[w2][h2+1] = dist[w2][h2];
                    que.push(P(w2,h2+1));
                }
                if((h2-1)>= 0 && (dist[w2][h2-1] - dist[w2][h2]) > 0){
                    dist[w2][h2-1] = dist[w2][h2];
                    que.push(P(w2,h2-1));
                }
            }
            else {
                if((w2-1)>= 0  && (dist[w2-1][h2] - dist[w2][h2]) > 1) {
                    dist[w2-1][h2] = dist[w2][h2] + 1;
                    que.push(P(w2-1,h2));
                }
                if((w2+1)<= N-1  && (dist[w2+1][h2] - dist[w2][h2] > 1)){
                    dist[w2+1][h2] = dist[w2][h2] + 1;
                    que.push(P(w2+1,h2));
                }
                if((h2+1)<= N-1 && (dist[w2][h2+1] - dist[w2][h2] > 1)){
                    dist[w2][h2+1] = dist[w2][h2] + 1;
                    que.push(P(w2,h2+1));
                }
                if((h2-1)>= 0 && (dist[w2][h2-1] - dist[w2][h2]) > 1){
                    dist[w2][h2-1] = dist[w2][h2] + 1;
                    que.push(P(w2,h2-1));
                }
            }
        }
    }
    cout << ans << endl;
    return 0;
}


Measured memory_bytes: 6084