#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define endl '\n'
#define all(x) (x).begin(),(x).end()
const int INF=500+5;
const int N=500+5;
const ll oo=1e18+5;
const ll mod=998244353;

int dp[N][N];
bool used[N][N];

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

int n;
bool InSide(int x,int y){
	return (x>=0&&x<n&&y>=0&&y<n);
}

void bfs(int x,int y){
	queue<pair<int,int>> Q;
	Q.push({x,y});
	while(!Q.empty()){
		int x=Q.front().first;
		int y=Q.front().second;
		Q.pop();
		for(int dir=0;dir<4;++dir){
			int new_x=x+dx[dir];
			int new_y=y+dy[dir];
			if(!InSide(new_x,new_y)) continue;
			if(dp[new_x][new_y]>dp[x][y]+(used[x][y]==1?0:1)){
				dp[new_x][new_y]=dp[x][y]+(used[x][y]==1?0:1);
				Q.push({new_x,new_y});
			}
		}
	}
}

int main(){
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin>>n;
	for(int i=0;i<n;++i){
		for(int j=0;j<n;++j){
			dp[i][j]=min({i,j,n-1-i,n-1-j});
		}
	}
	ll ans=0;
	for(int i=0;i<n*n;++i){
		int pos;
		cin>>pos;
		--pos;
		int x=pos/n;
		int y=pos%n;
		ans+=dp[x][y];
		used[x][y]=1;
		bfs(x,y);
	}
	cout<<ans;
	return 0;
}
