#include<bits/stdc++.h>
#define ll long long
#define pii_ pair<int,int>
#define mp_ make_pair
#define pb push_back
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define show1(a) cout<<#a<<" = "<<a<<endl
#define show2(a,b) cout<<#a<<" = "<<a<<"; "<<#b<<" = "<<b<<endl
using namespace std;
const ll INF = 1LL<<60;
const int inf = 1<<30;
const int maxn = 3e5+5;
inline void fastio() {ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);}

int n,out[505][505];
ll f[505][505],ans;
int dx[5] = {-1,1,0,0};
int dy[5] = {0,0,-1,1};
void dfs(int x,int y)
{
    rep(i,0,3){
        int nx = x+dx[i],ny = y+dy[i];
        if(nx>=1&&nx<=n&&ny>=1&&ny<=n&&!out[nx][ny]){
            if(f[nx][ny]==f[x][y]+2){
                f[nx][ny]--;
                dfs(nx,ny);
            }
        }
    }
}
int main()
{
    fastio();
    cin>>n;
    int x,y,s;
    rep(i,1,n) rep(j,1,n){
        f[i][j] = min(min(i-1,n-i),min(j-1,n-j));
    }
    ans = 0;
    rep(i,1,n*n){
        cin>>s;
        x = (s+n-1)/n;
        y = s-(x-1)*n;
        ans += f[x][y];
        f[x][y]--;
        out[x][y]=1
        dfs(x,y);
    }
    cout<<ans<<endl;
    return 0;
}
