#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define ld double
#define ll long long
#define all(x) x.begin(),x.end()
const int mod=1e9+7;
#define fi first
#define se second
#define Dracarys  ios_base::sync_with_stdio(false);cin.tie(NULL);
#define inf (ll)1e16

#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")

vector<vector<pair<ll,ll>>> adj;
vector<bitset<550>> check(505);

ll n;

inline ll name(ll xx,ll yy){
    return ((xx-1)*n+yy);
}

inline pair<ll,ll> cor(ll xx){
    xx--;
    return {(xx/n)+1,(xx%n+1)};
}

void setup(ll x){
    auto pr=cor(x);
    check[pr.fi][pr.se]=0;
    adj.clear();
    adj.resize(n*n+5);
    
    for (ll i=1;i<=n;i++){
        for (ll j=1;j<=n;j++){
            adj[name(i,j)].pb({name(i,j-1),check[i][j-1]+check[i][j]});
            adj[name(i,j)].pb({name(i-1,j),check[i-1][j]+check[i][j]});
            adj[name(i,j)].pb({name(i,j+1),check[i][j+1]+check[i][j]});
            adj[name(i,j)].pb({name(i+1,j),check[i+1][j]+check[i][j]});
        }
    }
}

vector<ll> edges;
ll hate=0;
void bfs(ll x){
    vector<ld> dis(n*n+5,inf);
    
    priority_queue<pair<ld,ll>,vector<pair<ld,ll>>,greater<pair<ld,ll>>> pq;
    pq.push({0.0,x});
    dis[x]=0.0;
    while(pq.size()){
        ll curr=pq.top().second;
        pq.pop();
        for (auto xd:adj[curr]){
            ll nxt=xd.fi;
            ld wt=xd.se+1e-6;
            ld ndis=dis[curr]+wt;
            if (ndis<dis[nxt]){
                dis[nxt]=ndis;
                pq.push({ndis,nxt});
            }
        }
    }
    
    ll mn=inf;
    for (auto xx:edges){
        auto pr=cor(xx);
        mn=min(mn,((ll)dis[xx]+check[pr.fi][pr.se])/2);
    }
    hate+=mn;
}

void solve(){
    cin>>n;
    
    ll sz=n*n;
    vector<ll> per(sz);
    for (ll i=0;i<sz;i++)
    cin>>per[i];
    
    for (ll i=1;i<=n;i++){
        edges.pb(name(i,1));
        edges.pb(name(1,i));
        edges.pb(name(n,i));
        edges.pb(name(i,n));
    }
    
    for (ll i=1;i<=n;i++){
        for (ll j=0;j<=n;j++){
            check[i][j]=1;
        }
    }
    
    for (ll i=0;i<sz;i++){
        setup(per[i]);
        bfs(per[i]);
    }
    cout<<hate<<"\n";
}

int main() {
    
    Dracarys
    
    ll t=1;
    //cin>>t;
    while(t--){ solve(); }
    
    return 0;
}