#include <bits/stdc++.h>
typedef long long ll;
using namespace std;
#define repr(i,a,b) for(int i=a;i<b;i++)
#define rep(i,n) for(int i=0;i<n;i++)
#define invrepr(i,a,b) for(int i=b-1;i>=a;i--)
#define invrep(i,n) invrepr(i,0,n)
#define repitr(itr,a) for(auto itr=a.begin();itr!=a.end();++itr)
#define P pair<int,int>
const ll MOD=1e9+7;
const ll INF=1e9;
const double PI=acos(-1);
const int MAX=200010;

int n;
vector<vector<int>> h(510,vector<int>(510,1)),s(510,vector<int>(510,1));
vector<int> di={-1,0,1,0},dj={0,-1,0,1};

void dfs(int i,int j) {
    int d=h[i][j]+s[i][j];
    rep(k,4) {
        int ni=i+di[k],nj=j+dj[k];
        if (ni>=0 && ni<n && nj>=0 && nj<n) {
            if (h[ni][nj]>d) {
                h[ni][nj]=d;
                dfs(ni,nj);
            }
        }
    }
}

int main() {
    ios_base::sync_with_stdio(false);

    cin >> n;
    ll ans=0;
    rep(i,n) {
        rep(j,n) h[i][j]=min(min(i,n-1-i),min(j,n-1-j));
    }
    rep(k,n*n) {
        int p;
        cin >> p;
        --p;
        int i=p/n,j=p%n;
        s[i][j]=0;
        ans+=h[i][j];
        dfs(i,j);
    }
    cout << ans << endl;

    return 0;
}
