#include <bits/stdc++.h>
#define mins(x,y) (x=min(x,y))
#define rep(i,n) for(int i=0; i<(n); i++)
using namespace std;
using ll = long long;
using P  = pair<int,int>;
using v  = vector<int>;
using vv = vector<v>;

int main(){
    int n;
    cin >> n;
    
    vv cost(n,v (n));
    vv seat(n,v (n,1));
    
    rep(i,n)rep(j,n){
        cost[i][j] = i;
        mins(cost[i][j],j);
        mins(cost[i][j],n-1-i);
        mins(cost[i][j],n-1-j);
    }
    
    int ans = 0;
    
    v di={-1,0,1,0};
    v dj={0,-1,0,1};//四方
    
    for(int i=0; i<n*n; i++){
        int p;
        cin >> p;
        p--;
        int pi = p/n;
        int pj = p%n;
        
        ans += cost[pi][pj];
        seat[pi][pj] = 0;
        
        //幅優先で距離リストを更新
        queue<P> more;
        more.push(pi,pj);
        
        while(!more.empty()){
            
            int ni = more.front().first;
            int nj = more.front().second;
            int nc = cost[ni][nj]+seat[ni][nj];
            
            for(int l=0; l<4; l++){
                int mi = ni+di[l], mj = nj+dj[l];
                if(mi>=n || mi<0 || mj>=n || mj<0)continue;
                if(cost[mi][mj]<=nc)continue;
                
                cost[mi][mj] = nc;
                more.push(mi,mj);
                
            }
            more.pop();
        }
    }
    
    cout << ans << endl;
    return 0;
}