#include <bits/stdc++.h>
using namespace std;
typedef pair<int,int> ii;
int n;
int g[505][505];
int ans = 0;
queue<ii> q;
int d[505][505];
int dx[] = {1,0,-1,0}, dy[] = {0,1,0,-1};
int main(){
    scanf("%d",&n);
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            g[i][j] = min(min(i+1,n-i),min(j+1,n-j));
            d[i][j] = 1;
        }
    }
    for (int i =0;  i < n*n; i++){
        int X;
        scanf("%d",&X);
        X--;
        int _x = X/n, _y = X%n;
        g[_x][_y]--;
        d[_x][_y] = 0;
        ans += g[_x][_y];
        //printf("%d %d takes %d\n",_x,_y,g[_x][_y]);
        q.push({_x,_y});
        while (q.size()){
            int x,y;
            tie(x,y) = q.front(); q.pop();
            for (int k = 0; k < 4; k++){
                int nx = x+dx[k], ny = y+dy[k];
                if (nx >= 0 && nx < n && ny >= 0 && ny < n){
                    if (g[nx][ny] > g[x][y]+d[nx][ny]){
                        g[nx][ny] = g[x][y]+d[nx][ny];
                        q.push({nx,ny});
                    }
                }
            }
        }
    }
    printf("%d",ans);


}
