#include<bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>  
#include <ext/pb_ds/tree_policy.hpp>   
using namespace std;
using namespace __gnu_pbds;  
typedef tree<int, null_type, less<int>, rb_tree_tag,  tree_order_statistics_node_update>  ordered_set; 
#define num 1000000007
//#define num 998244353
#define REP(i,a,n) for(ll i=a;i<n;i++)
#define inarr(i,a,n,A) for(ll i=a;i<n;i++) { cin>>A[i]; }
#define vll vector<long long>
#define pii pair <int,int>
#define pll pair<long long, long long>
#define vpii vector< pair <int,int>>
#define vpll vector< pair<long long, long long>>
#define avg(a,b) ((a)+(b))/2
#define pb push_back
#define pob pop_back
#define sf int flag=0     //Remember semicolon
#define assmin(a,b) if(b<=a) { a=b; }
#define assmax(a,b) if(b>=a) { a=b; }
#define f first
#define s second
#define fix(f,n) std::fixed<<std::setprecision(n)<<f
#define all(x) x.begin(), x.end()
#define M_PI 3.14159265358979323846
#define epsilon (long double)(0.00000000001)
#define side 1e9
#define watch(x) cout << (#x) << " is " << (x) << endl
typedef long long ll;

ll mod(ll a, ll b)
{
	if(a%b<0)
	{
		return a%b + b;
	}
	return a%b;
}
 
 
ll mod_exp(ll a, ll b, ll c)
{
	ll res=1;
	a=a%c;
	while(b>0)
	{
		if(b%2==1)
		{
			res=(res*a)%c;
		}
		b/=2;
		a=(a*a)%c;
	}
	return res;
}
ll mymod(ll a,ll b)
{
    return ((a%b)+b)%b;
}
bool prime[200001];
void SieveOfEratosthenes() 
{ 
    memset(prime, true, sizeof(prime)); 
    prime[1]=false;
    for (int p=2; p*p<=200000; p++) 
    { 
        if (prime[p] == true) 
        { 
            for (int i=p*p; i<=200000; i += p) 
                prime[i] = false; 
        } 
    } 
    return;
}
ll powe[100005];
void power()
{
    powe[0]=1;
    REP(i,1,100005)
    {
        powe[i]=mymod(2*powe[i-1],num);
    }
}
ll gcdExtended(ll,ll,ll *,ll *);
ll modInverse(ll a, ll m) 
{ 
    ll x, y; 
    ll g = gcdExtended(a, m, &x, &y); 
        ll res = (x%m + m) % m;
        return res;
} 
ll gcdExtended(ll a, ll b, ll *x, ll *y) 
{ 
    if (a == 0) 
    { 
        *x = 0, *y = 1; 
        return b; 
    } 
    ll x1, y1; 
    ll gcd = gcdExtended(b%a, a, &x1, &y1); 
    *x = y1 - (b/a) * x1; 
    *y = x1; 
    return gcd; 
}
struct node
{
	vll adj;
};


ll n;
ll f(ll i, ll j)
{
    ll minm = i;
    minm = min(minm, n-1-i);
    minm = min(minm, j);
    minm = min(minm, n-1-j);
    return minm;
}


bool valid(ll a)
{
    return a>=1 && a<=n*n;
}
ll f1(ll a)
{
    return (a-1)/n;
}
ll f2(ll a)
{
    return (a%n-1 + n)%n;
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cin>>n;
    int parent[n][n];
    int distwall[n][n];
    ll tot=0;
    REP(i,0,n)
    {
        REP(j,0,n)
        {
            parent[i][j] = n*i + j + 1;
            distwall[i][j] = f(i,j);
        }
    }
    bool vis[n][n]={};
    REP(i,0,n*n)
    {
        ll f;
        cin>>f;
       // watch(f);
        tot+=distwall[f1(f)][f2(f)];
        vis[f1(f)][f2(f)] = true;
       // watch(tot);
        queue<ll> q;
        q.push(f);
        while(!q.empty())
        {
            ll a = q.front();
            q.pop();
            //watch(a);
            ll d = distwall[f1(a)][f2(a)];
            if(f2(a)!=0)
            {
                if(distwall[f1(a-1)][f2(a-1)] > ((vis[f1(a)][f2(a)])? d : d+1)) 
                {
                    distwall[f1(a-1)][f2(a-1)]--;
                    q.push(f1(a+1)*n + f2(a+1) + 1);
                }
            }
            if(f2(a)!=n-1)
            {
                if(distwall[f1(a+1)][f2(a+1)] >((vis[f1(a)][f2(a)])? d : d+1))
                {
                    distwall[f1(a+1)][f2(a+1)]--;
                    q.push(f1(a+1)*n + f2(a+1) + 1);
                }
            }
            if(f1(a)!=0)
            {
                if(distwall[f1(a-n)][f2(a-n)] >((vis[f1(a)][f2(a)])? d : d+1))
                {
                    distwall[f1(a-n)][f2(a-n)]--;
                    q.push(f1(a-n)*n + f2(a-n) + 1);
                }
            }
            if(f1(a)!=n-1)
            {
                if(distwall[f1(a+n)][f2(a+n)] >((vis[f1(a)][f2(a)])? d : d+1))
                {
                    distwall[f1(a+n)][f2(a+n)]--;
                    q.push(f1(a+n)*n + f2(a+n) + 1);
                }
            }
        }
    }
    cout<<tot<<"\n";
    return 0;
}


