#include<bits/stdc++.h>
using namespace std;
#define ref(i,n) for(int i=1;i<=n;i++)

int n,c[500][500],used[500][500];




void upd(int x,int y){
    if(x-1>=0&&c[x][y]+1-used[x][y]<c[x-1][y]) {c[x-1][y]=c[x][y]+1-used[x][y];upd(x-1,y);}
    if(y-1>=0&&c[x][y]+1-used[x][y]<c[x][y-1]) {c[x][y-1]=c[x][y]+1-used[x][y];upd(x,y-1);}
    if(y+1<n&&c[x][y]+1-used[x][y]<c[x][y+1]) {c[x][y+1]=c[x][y]+1-used[x][y];upd(x,y+1); }
    if(x+1<n&&c[x][y]+1-used[x][y]<c[x+1][y]) {c[x+1][y]=c[x][y]+1-used[x][y];upd(x+1,y); }
}

int main()
{
    cin>>n;

    int nn=pow(n,2);

    ref(i,nn) {

        int u=(i-1)%n,v=(i-1)/n;
        c[u][v]=min(min(u,n-1-u),min(v,n-1-v));

    }

    int ans=0;
    ref(i,nn){
        int y;
        cin>>y;


        int u=(y-1)%n,v=(y-1)/n;


        used[u][v]=1;
        ans+=c[u][v];
        upd(u,v);
    }
    cout<<ans;
    return 0;
}
