#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define REP(i,n) for(ll i = 0; i<(ll)(n); i++)
#define REPS(i,n) for (ll i = 1; i<=(ll)(n); i++)
#define REPD(i,n) for(ll i=(ll)(n)-1;i>=0;i--)
#define FOR(i,a,b) for(ll i=(a);i<=(b);i++)
#define FORD(i,a,b) for(ll i=(a);i>=(b);i--)
#define pb push_back
#define MOD 1000000007
#define MOD2 998244353
#define PI 3.141592653
#define INF 100000000000000
#define N 550

vector<vector<ll>> adj={{0,1},{1,0},{0,-1},{-1,0}};
vector<vector<ll>> seats(N,vector<ll>(N,-1));
vector<vector<ll>> soon(N,vector<ll>(N,0));

void dfs(ll x, ll y){
    if (soon[x][y]==1) seats[x][y]--;
    REP(i,4)if (seats[x+adj[i][0]][y+adj[i][1]]>seats[x][y]) dfs(nex,ney);
    if (soon[x][y]==0) seats[x][y]--;
}

int main(){
    ll n, p, ind, ans; cin >> n;
    ind=1; ans = 0;
    while(ind<=n+1-ind){
        FOR(i,ind,n+1-ind)FOR(j,ind,n+1-ind) seats[i][j]=ind-1;
        ind++;
    }
    REP(i,n*n){
        cin >> p;
        ll h = (p-1)/n+1; ll w = (p-1)%n+1;
        ans+=seats[h][w];
        soon[h][w]=1;
        REP(j,4) if (seats[h+adj[j][0]][w+adj[j][1]]>seats[h][w]) dfs1(adh,adw);
    }
    cout << ans << endl;
}