//B 
#include <bits/stdc++.h>
#define mp(a,b) make_pair(a,b) 
using namespace std;
typedef long long ll;
typedef pair<int,int> Node; 
const int maxn = 505;
const int INF = 1e9;
bool G[maxn][maxn],vis[maxn][maxn];
int dis[maxn][maxn];
int p[maxn*maxn],n;
void spfa(int sx,int sy){
//	G[sx][sy] = 1;
	queue<Node>q;
	q.push(mp(sx,sy));
	dis[sx][sy] = 0;
	vis[sx][sy] = 1;
	while(!q.empty()){
		Node u = q.front();
		q.pop();
		int ux = u.first,uy = u.second;
		vis[ux][uy] = 0;
//		cout << ux << " " << uy << endl;
		for(int i = -1;i <= 1;i++)
			for(int j = -1;j <= 1;j++)if(abs(i+j) == 1){
				int vx = ux+i,vy = uy+j;
				if(vx < 0 or vx > n+1 or vy < 0 or vy > n+1)continue;
				if(dis[vx][vy] > dis[ux][uy] + (!G[vx][vy])){
					dis[vx][vy] = dis[ux][uy] + (!G[vx][vy]);
					if(!vis[vx][vy]){
						vis[vx][vy] = 1;
						q.push(mp(vx,vy));
					}
				}
			}
	}
	G[sx][sy] = 1;
}
int main(){
	cin >> n;
	ll ans = 0;
	for(int i = 0;i <= n+1;i++)G[0][i] = G[n+1][i] = G[i][0] = G[i][n+1] = 1;
	for(int t = 1;t <= n*n;t++){
		int x;
		cin >> x;
		int sx = (x-1)/n+1,sy = x%n;
		if(!sy)sy = n;
		for(int i = 0;i <= n+1;i++)
			for(int j = 0;j <= n+1;j++){
				dis[i][j] = INF;
				vis[i][j] = 0;
			}
		spfa(sx,sy);
		for(int i = 0;i <= n+1;i++)tmp = min({tmp,dis[0][i],dis[n+1][i],dis[i][0],dis[i][n+1]});
		ans += dis[0][0];
//		for(int i = 0;i <= n+1;i++){
//			for(int j = 0;j <= n+1;j++)cout << G[i][j] << " ";
//			cout << endl;
//		}
//		cout << t << " " << tmp << " " << dis[0][0] << endl;
//		cout << endl;
	}
	cout << ans;
	return 0;
}
