#include<bits/stdc++.h>
#define cs const
#define pb push_back
using namespace std;
cs int N = 550, M = N * N;
int n, d[N][N]; bool bn[N][N];
int main(){
	#ifdef FSYolanda
	freopen("1.in","r",stdin);
	#endif
	scanf("%d",&n);
	for(int i=1; i<=n; i++)
	for(int j=1; j<=n; j++)
	d[i][j]=min(min(i-1,j-1),min(n-i,n-j));
	int ans = 0; queue<int> q;
	for(int i=1,t,x,y; i<=n*n; i++){
		scanf("%d",&t); x=(t-1)/n+1, y=t-(x-1)*n;
		ans += d[x][y]; bn[x][y] = true;
		static int dx[4]={1,-1,0,0};
		static int dy[4]={0,0,1,-1};
		q.push(t);
		while(!q.empty()){
			int now = q.front(); q.pop();
			int nx = (now-1)/n+1, ny = now-(nx-1)*n;
			for(int i=0; i<4; i++){
				int tx = nx + dx[i], ty = ny + dy[i];
				if(d[tx][ty] > d[nx][ny] + 1 - bn[nx][ny]){
					d[tx][ty] = d[nx][ny] + 1 - bn[nx][ny];
					q.push((tx-1)*n+ty);
				}
			}
		}
	} cout << ans; return 0;
}