#include<bits/stdc++.h>
using namespace std;
const int N=250050;
int n,a[N],dx[4]={-1,0,0,1},dy[4]={0,1,-1,0},d[N],q[N],l,r,ans;bool vis[N];
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n*n;i++)scanf("%d",&a[i]);
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
            d[(i-1)*n+j]=min(min(i-1,n-i),min(j-1,n-j));
    for(int i=1;i<=n*n;i++){
        q[l=r=1]=a[i];ans+=d[a[i]];vis[a[i]]=1;
        while(l<=r){
            int id=q[l++],x=(id-1)/n+1,y=(id-1)%n+1;
            for(int j=0;j<4;j++){
                int xx=x+dx[j],yy=y+dy[j],iid=(xx-1)*n+yy;
                if(xx&&xx<=n&&yy&&yy<=n&&d[iid]>d[id]+!vis[id])
                    d[iid]=d[id]+!vis[id],q[++r]=iid;
            }
        }
    }
    cout<<ans;
    return 0;
}