#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef pair<int,int> pii;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define sz(a) int(a.size())
const int N=510;

int gi() {
	int x=0,o=1;char ch=getchar();
	while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
	if(ch=='-') o=-1,ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*o;
}

int n,dis[N][N],val[N][N],dx[4]={1,0,0,-1},dy[4]={0,1,-1,0};
queue<pii> q;

bool in(int x,int y) {
	return 1<=x&&x<=n&&1<=y&&y<=n;
}

int main() {
	cin>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++) val[i][j]=1,dis[i][j]=min(i,min(j,min(n-i+1,n-j+1)));
	int ans=0;
	for(int i=1;i<=n*n;i++) {
		int id=gi(),x=(id-1)/n+1,y=(id-1)%n+1;
		ans+=dis[x][y];--dis[x][y];q.push(mp(x,y));
		val[x][y]=0;
		while(!q.empty()) {
			int x=q.front().fi,y=q.front().se;
			q.pop();
			for(int d=0;d<4;d++) {
				int xx=x+dx[d],yy=y+dy[d];
				if(!in(xx,yy)) continue;
				if(dis[xx][yy]>dis[x][y]+val[xx][yy]) dis[xx][yy]=dis[x][y]+val[xx][yy],q.push(mp(xx,yy));
			}
		}
	}
	cout<<ans-n*n;
	return 0;
}
