//#include <bits/stdc++.h>
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
//#include "boost/multiprecision/cpp_int.hpp"
//typedef boost::multiprecision::cpp_int LL;
typedef long double dd;
#define i_7 (ll)(1E9+7)
//#define i_7 998244353
#define i_5 i_7-2
ll mod(ll a){
    ll c=a%i_7;
    if(c>=0)return c;
    return c+i_7;
}
typedef pair<ll,ll> l_l;
typedef pair<dd,dd> d_d;
ll inf=(ll)1E16;
#define rep(i,l,r) for(ll i=l;i<=r;i++)
#define pb push_back
ll max(ll a,ll b){if(a<b)return b;else return a;}
ll min(ll a,ll b){if(a>b)return b;else return a;}
void Max(ll &pos,ll val){pos=max(pos,val);}//Max(dp[n],dp[n-1]);
void Min(ll &pos,ll val){pos=min(pos,val);}
void Add(ll &pos,ll val){pos=mod(pos+val);}
dd EPS=1E-9;
#define fastio ios::sync_with_stdio(false); cin.tie(0); cout.tie(0);
#define fi first
#define se second
#define endl "\n"
#define SORT(v) sort(v.begin(),v.end())
#define ERASE(v) v.erase(unique(v.begin(),v.end()),v.end())
#define POSL(v,x) (lower_bound(v.begin(),v.end(),x)-v.begin())
#define POSU(v,x) (upper_bound(v.begin(),v.end(),x)-v.begin())
template<class T>
inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return true;
    }
    return false;
}
template<class T>
inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return true;
    }
    return false;
}

void mod_print(ll k){
    ll P=1000;
    rep(y,1,P){
        ll x=mod(y*k);
        if(x+P>=i_7){
            x-=i_7;
        }
        if(abs(x)<=P){
            cout<<x<<"/"<<y<<endl;
            return;
        }
    }
    cout<<"nun"<<endl;
}
//////////////////////////

struct pos{
    ll cnt;
    ll x,y;
    bool operator>(const pos &opp) const{return cnt > opp.cnt;}
    bool operator<(const pos &opp) const{return cnt < opp.cnt;}
};
ll n;
ll dx[4]={1,0,-1,0};
ll dy[4]={0,1,0,-1};
bool valid(ll i,ll j){
    if(0<=i&&i<=n-1&&0<=j&&j<=n-1)return true;
    return false;
}

int main(){fastio
    cin>>n;
    ll a[n*n];
    rep(i,0,n*n-1){
        cin>>a[i];a[i]--;
    }
    bool left[n][n];memset(left,false,sizeof(left));
    ll dp[n][n];
    
    priority_queue<pos,vector<pos>,greater<pos>>q;
    rep(i,0,n-1)rep(j,0,n-1){
        dp[i][j]=inf;
        if(i==0||i==n-1||j==0||j==n-1){
            dp[i][j]=0;
            q.push({0,i,j});
        }
    }
    while(!q.empty()){
        pos top=q.top();q.pop();
        //cout<<top.cnt<<endl;
        rep(z,0,3){
            ll nx=top.x+dx[z];
            ll ny=top.y+dy[z];
            if(!valid(nx,ny))continue;
            if(chmin(dp[nx][ny],dp[top.x][top.y]+1)){
                q.push({dp[nx][ny],nx,ny});
            }
        }
    }
    //rep(i,0,n-1){rep(j,0,n-1)cout<<dp[i][j]<<" ";cout<<endl;}
    ll ans=0;
    rep(i,0,n*n-1){
        ll px=a[i]/n;
        ll py=a[i]%n;
        ans+=dp[px][py];
        //cout<<a[i]<<" "<<dp[px][py]<<endl;
        left[px][py]=true;
        priority_queue<pos,vector<pos>,greater<pos>>q;
        q.push({dp[px][py],px,py});
        while(!q.empty()){
            pos top=q.top();q.pop();
            //cout<<top.cnt<<endl;
            rep(z,0,3){
                ll nx=top.x+dx[z];
                ll ny=top.y+dy[z];
                if(!valid(nx,ny))continue;
                if(left[top.x][top.y]){
                    if(chmin(dp[nx][ny],dp[top.x][top.y])){
                        q.push({dp[nx][ny],nx,ny});
                    }
                }else{
                    if(chmin(dp[nx][ny],dp[top.x][top.y]+1)){
                        q.push({dp[nx][ny],nx,ny});
                    }
                }
            }
            
        }
    }
    cout<<ans<<endl;
    
    return 0;
}


Measured memory_bytes: 12920