#include <algorithm>
#include <cassert>
#include <cstdio>
#include <cstring>
const int N=250001;
int n, dis[N], q[N];
bool vis[N];
bool cmin(int &a, int b) {
	return b<a?(a=b, true):false;
}
int main() {
	scanf("%d", &n);
	for(int i=1; i<=n; ++i) {
		for(int j=1; j<=n; ++j) {
			dis[i*n+j-n]=std::min(std::min(i, n-i+1), std::min(j, n-j+1));
			vis[i*n+j-n]=true;
		}
	}
	int ans=0;
	for(int i=1; i<=n*n; ++i) {
		int p, *qhead=q, *qtail=q;
		scanf("%d", &p);
		*qtail++=p;
		ans+=--dis[p];
		vis[p]=false;
		while(qhead<qtail) {
			int u=*qhead++;
			if(u>n && cmin(dis[u-n], dis[u]+vis[u-n])) *qtail++ = u-n;
			if(u<=n*n-n && cmin(dis[u+n], dis[u]+vis[u+n])) *qtail++ = u+n;
			if(u%n!=1 && cmin(dis[u-1], dis[u]+vis[u-1])) *qtail++ = u-1;
			if(u%n!=0 && cmin(dis[u+1], dis[u]+vis[u+1])) *qtail++ = u+1;
		}
	}
	printf("%d\n", ans);
	return 0;
}
