#include <iostream>
using namespace std;int f[4]={1,0,-1,0},F[4]={0,1,0,-1},I,n,p,x,y,i,j,d[505][505],a,w[505][505];void D(int x,int y){for(int i=0;i<4;++i){int X=x+f[i],Y=y+F[i];if(d[X][Y]>d[x][y]+w[X][Y])d[X][Y]=d[x][y]+w[X][Y],D(X,Y);}}int main(){cin>>n;for(I=0;I<n*n;++I){i=I/n+1,j=I%n+1;w[i][j]=1,d[i][j]=min(min(i,n-i+1),min(j,n-j+1));}for(i=1;i<=n*n;++i){cin>>p;p--;x=p/n+1;y=p%n+1;a+=--d[x][y];w[x][y]=0;D(x,y);}cout<<a<<endl;}