#include<bits/stdc++.h>
using namespace std;
using ll=long long;
template<class T,class U> inline bool chmin(T&x,U y){if(x>y){x=y;return true;}return false;}
template<class T,class U> inline bool chmax(T&x,U y){if(x<y){x=y;return true;}return false;}
#define fr(i,n) for(int i=0;i<(n);++i)
#define Fr(i,n) for(int i=1;i<=(n);++i)
#define ifr(i,n) for(int i=(n)-1;i>=0;--i)
#define iFr(i,n) for(int i=(n);i>0;--i)

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    vector<int> p(n*n),m(n*n);
    vector<bool> v(n*n);
    for(auto&i:p) cin>>i,--i;
    fr(i,n) fr(j,n) m[i*n+j]=min({i,j,n-1-i,n-1-j});
    int ans{};
    for(auto l:p){
        ans+=m[l];
        queue<int> q;
        q.emplace(l);
        v[l]=true;
        while(!q.empty()){
            int i=q.front();q.pop();
            int D=m[i];
            if(!v[i]) ++D;
            if(i/n and m[i-n]>D) m[i-n]=D,q.push(i-n);
            if(i%n and m[i-1]>D) m[i-1]=D,q.push(i-1);
            if(i<n*n-n and m[i+n]>D) m[i+n]=D,q.push(i+n);
            if(i%n<n-1 and m[i+1]>D) m[i+1]=D,q.push(i+1);
        }
    }
    cout<<ans<<'\n';
}