#include<bits/stdc++.h>
using namespace std;
#define int long long
#define forinc(i,a,b) for(int i=a;i<=b;++i)
#define fordec(i,a,b) for(int i=a;i>=b;--i)
#define fi first
#define se second
#define pii pair<int,int>

const int maxn=25010;
int n,a[maxn];
int d[510][510],vis[510][510],ret;
int dk[]={-1,0,1,0};
int ck[]={0,1,0,-1};

void bfs(int u,int v){
    int tmp=d[u][v]+vis[u][v];
    forinc(t,0,3){
        int x=u+dk[t],y=v+ck[t];
        if(d[x][y]>tmp){
            d[x][y]=tmp;
            bfs(x,y);
        }
    }
}

main(){
  //  freopen("test.inp","r",stdin);
    ios_base::sync_with_stdio(false);
    cin>>n;
    forinc(i,1,n*n) cin>>a[i];
    forinc(i,1,n) forinc(j,1,n){
        d[i][j]=min(min(i,n-i+1),min(j,n-j+1))-1;
        vis[i][j]=1;
    }
    forinc(i,1,n*n){
        int u=(a[i]+n-1)/n;
        int v=a[i]%n;
        if(v==0) v=n;
        vis[u][v]=0;
        ret+=d[u][v];
        bfs(u,v);
    }
    cout<<ret;
}

