#include<bits/stdc++.h>
#define For(a,b,c) for(int a=b;a<=c;++a)
#define Dor(a,b,c) for(int a=b;a>=c;--a)
using namespace std;
typedef long long LL;
const int N=507;
int n,F[N][N],H[N][N];
bool eat(int &f,int g) {
	if(g<f) return f=g,1;
	return 0;
}
void fly(int x,int y) {
	if(eat(F[x+1][y],F[x][y]+H[x+1][y])) fly(x+1,y);
	if(eat(F[x-1][y],F[x][y]+H[x-1][y])) fly(x-1,y);
	if(eat(F[x][y+1],F[x][y]+H[x][y+1])) fly(x,y+1);
	if(eat(F[x][y-1],F[x][y]+H[x][y-1])) fly(x,y-1);
}
int main() {
	scanf("%d",&n);
	For(i,1,n) For(j,1,n) F[i][j]=min(min(i,j),min(n-i+1,n-j+1)),H[i][j]=1;
	int ans=0;
	For(i,1,n*n) {
		int x,a,b;
		scanf("%d",&x),a=(x-1)/n+1,b=(x-1)%n+1;
		--F[a][b],fly(a,b);
		ans+=F[a][b],H[a][b]=0;
	}
	printf("%d",ans);
	return 0;
}