#include <bits/stdc++.h>
#define rep(i,n)for(int i=0;i<(n);i++)
using namespace std;
typedef long long ll;
typedef pair<int,int>P;

const int MOD=1000000007;
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;

int p[600*600];
int d[600][600];
bool b[600][600];

int dx[]{1,-1,0,0},dy[]{0,0,1,-1};

int main(){
	int n;cin>>n;
	rep(i,n*n)scanf("%d",&p[i]),p[i]--;
	rep(i,n)rep(j,n){
		d[i][j]=min({i,j,n-i-1,n-j-1});
		b[i][j]=true;
	}
	ll ans=0;
	rep(i,n*n){
		int x=p[i]/n,y=p[i]%n;
		ans+=d[x][y];
		//~ if(d[x][y]){
			//~ cout<<"-----"<<i<<' '<<p[i]<<endl;
			//~ rep(i,n){
				//~ rep(j,n)cout<<b[i][j]<<' ';
				//~ cout<<endl;
			//~ }
		//~ }
		b[x][y]=false;
		deque<P>que;
		que.push_back(P(x,y));
		while(!que.empty()){
			P p=que.front();que.pop_front();
			rep(k,4){
				int nx=p.first+dx[k],ny=p.second+dy[k];
				if(0<=nx&&nx<n&&0<=ny&&ny<n){
					int c=d[p.first][p.second]+b[p.first][p.second];
					if(d[nx][ny]>c){
						d[nx][ny]=c;
						if(b[p.first][p.second])que.push_back(P(nx,ny));
						else que.push_front(P(nx,ny));
					}
				}
			}
		}
		//~ puts("");
	}
	cout<<ans<<endl;
}