#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
const ll mod=1000000007;
/* 4 方向への隣接頂点への移動を表すベクトル */
const int dx[4] = { 1, 0, -1, 0 };
const int dy[4] = { 0, 1, 0, -1 };
int main(){
ll N; cin>>N;
ll field[N][N]={};
vector<vector<ll> > dist(N, vector<ll>(N, -1)); 
for(ll i=0; i<N;i++){
for(ll j=0; j<N;j++){
field[i][j]=1;
dist[i][j]=min(i,j);
dist[i][j]=min(dist[i][j],N-1-i);
dist[i][j]=min(dist[i][j],N-1-j);}}
ll p,a,b;
ll counter=0;
ll ca=0;
ll cb=100000;
for(ll i=0; i<N*N;i++){
cin>>p; p--;
a=p/N; 
b=p%N;
field[a][b]=0;
cb=100000;
ca+=dist[a][b];
    deque<pair<ll,ll> > que; 
    que.push_front(make_pair(a, b)); 
  
    while (!que.empty()) {
        pair<ll, ll> current_pos = que.front(); // キューの先頭を見る (C++ ではこれをしても pop しない)
        ll x = current_pos.first;
        ll y = current_pos.second;
        que.pop_front(); // キューから pop を忘れずに
        if(field[x][y]==1){
        for (ll direction = 0; direction < 4; ++direction) {
            ll next_x = x + dx[direction];
            ll next_y = y + dy[direction];
         
            if(dist[next_x][next_y]>dist[x][y]+1){
              dist[next_x][next_y]=dist[x][y]+1;
              que.push_front(make_pair(next_x, next_y));}}}
          
        if(field[x][y]==0){
        for (ll direction = 0; direction < 4; ++direction) {
            ll next_x = x + dx[direction];
            ll next_y = y + dy[direction];
         
            if(dist[next_x][next_y]>dist[x][y]){
              dist[next_x][next_y]=dist[x][y];
              que.push_front(make_pair(next_x, next_y));}}}
    }}
   cout << ca << endl;}
