#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// #include <ext/pb_ds/tree_policy.hpp>
// #include <ext/pb_ds/detail/standard_policies.hpp>
// using namespace __gnu_pbds;
using namespace std;
// template <typename T>
// using ordered_set = tree<T, null_type, less<T>, rb_tree_tag,
//     tree_order_statistics_node_update>;
 
// #pragma GCC optimize("Ofast")
// #pragma GCC target("avx,avx2,fma")
// #pragma GCC optimization ("unroll-loops")
typedef long long int ll;
const ll N = 1e6;
// const ll m = 1e9 + 7;
// const ll inf= 1e14;
const ll mod =1e9 + 7;
const ll INF = 1e14;
#define x first
#define y second
#define pb push_back
#define mp make_pair
#define pld pair<ld,ld>
#define pll pair<ll, ll>
#define pii pair<int, int>
#define ld long double
ll powm(ll a, ll b) { a = a%mod;ll res = 1; while (b) { if (b & 1) res = (res * a) % mod; a = (a * a) % mod; b >>= 1; } return res; }


int main() {   
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    #ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
    freopen("error.txt","w",stderr);
    #endif
    ll t = 1;
    // cin>>t; 
    while(t--){
        ll n;cin>>n;
        ll p[n*n];
        ll dp[n][n];
        bool is[n][n];
        for(int i = 0;i<n*n;i++){
            cin>>p[i];
        }
        for(ll i = 0;i<n;i++){
            for(ll j = 0;j<n;j++){
                dp[i][j] = min(i,n-i-1);
                dp[i][j] = min(dp[i][j],j);
                dp[i][j] = min(dp[i][j],n-j-1);
                is[i][j] = 1;
            }
        }
        ll ans = 0;
        queue<pii> q;
        for(int i = 0;i<n*n;i++){
            p[i]--;
            ll x = p[i]/n,y = p[i]%n;
            // cout<<x<<" "<<y<<'\n';
            ans+=dp[x][y];
            q.push(mp(x,y));
            is[x][y] = 0;
            while(!q.empty()){
                pii v = q.front();q.pop();
                if(v.x>0){
                    if(dp[v.x][v.y]+is[v.x][v.y]<dp[v.x-1][v.y]){
                        dp[v.x-1][v.y] = dp[v.x][v.y]+is[v.x][v.y];
                        q.push(mp(v.x-1,v.y));
                    }
                }
                if(v.x<n-1){
                    if(dp[v.x][v.y]+is[v.x][v.y]<dp[v.x+1][v.y]){
                        dp[v.x+1][v.y] = dp[v.x][v.y]+is[v.x][v.y];
                        q.push(mp(v.x+1,v.y));
                    }
                }
                if(v.y<n-1){
                    if(dp[v.x][v.y]+is[v.x][v.y]<dp[v.x][v.y+1]){
                        dp[v.x][v.y+1] = dp[v.x][v.y]+is[v.x][v.y];
                        q.push(mp(v.x,v.y+1));
                    }
                }
                if(v.y>0){
                    if(dp[v.x][v.y]+is[v.x][v.y]<dp[v.x][v.y-1]){
                        dp[v.x][v.y-1] = dp[v.x][v.y]+is[v.x][v.y];
                        q.push(mp(v.x,v.y-1));
                    }
                }
            }
            // for(ll i = 0;i<n;i++){
            //     for(ll j = 0;j<n;j++){
            //         cout<<dp[i][j]<<" ";
            //     }
            //     cout<<'\n';
            // }
            // cout<<'\n';
        }
        cout<<ans<<'\n';
    }
}              

Measured memory_bytes: 7776