X[3<<17],Y[3<<17];
d[4]={1,-1};
N;s;i;
f(x,y,c,r){for(c=X[x]+1-Y[x],r=4;r--;)0<=(y=x+d[r])&y<N*N&abs(x%N-y%N)<2&&X[y]>c&&f(y,X[y]=c);}
main(P){
	scanf("%d",&N);
	for(d[2]=N,d[3]=-N;i<N*N;i++)X[i]=i%N<i/N?fmin(i%N,N-i/N-1):fmin(i/N,N-i%N-1);
	for(;~scanf("%d",&P);)f(P,Y[P]=1,s+=X[--P]);
	printf("%d",s);
}