#include <iostream>
#include <fstream>
#include <cstdio>
#include <cassert>
#include <complex>
#include <cmath>
#include <algorithm>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <numeric>
#include <sstream>
#include <ctime>
#include <cctype>
#include <set>
#include <map>
#include <queue>
#include <bitset>
#include <deque>
#include <stack>
#include <memory.h>
using namespace std;
typedef long long ll;
#define mp make_pair

const ll N = 100005;

ll dx[4] = {0,0,1,-1};
ll dy[4] = {1,-1,0,0};

bool check(ll x,ll y, ll n, ll m){
    return x>=0 && y>=0 && x<n && y<m;
}
int main(){
    ll n;
    cin >> n;
    ll p = n * n;
    ll P[p];
    for(int i=0;i<p;i++){
		cin >> P[i];	
	}
    ll D[n][n];
    ll vis[n][n];
    for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
	    	ll a = min(i,n-i-1);
	    	ll b = min(j,n-j-1);
	    	D[i][j]=min(a,b);
	    	vis[i][j]=1;
    	}
    }
    ll ans=0;
    for(int i=0;i<p;i++){
    	ll r = P[i];
    	ll x = (r-1)/n;
    	ll y = (r-1)%n;
    	queue<pll> q;
    	q.push({x,y});
    	ans += D[x][y];
    	vis[x][y] = 0;
    	while(!q.empty()){
    		pll r = q.front();
    		q.pop();
    		for(int j=0;j<4;j++){
    			ll nx = r.ft+dx[j];
    			ll ny = r.sd+dy[j];
    			if(check(nx,ny,n,n) and D[nx][ny] > D[r.ft][r.sd] + vis[r.ft][r.sd]){
    				q.push({nx, ny});
    				D[nx][ny] = D[r.ft][r.sd] + vis[r.ft][r.sd];
    			}
    		}	
    	}
    }
    
    cout<<ans<<endl;
    return 0;
}