#include <bits/stdc++.h>
using namespace std;
#define rep(i,n) for(int i=0; i<(n); i++)
int n, p[505][505], q, x, y, ans, dx[] = {0,0,1,-1}, dy[] = {1,-1,0,0}, used[505][505];
void dfs(int x, int y){
	p[x][y]--;
	rep(i,4){
		int nx = x + dx[i], ny = y + dy[i];
		if(p[nx][ny] + used[nx][ny] == p[x][y] + 2) dfs(nx,ny);
	}
}
int main(){
	scanf("%d", &n);
	ans = -n * n;
	rep(i,n) rep(j,n) p[i+1][j+1] = min({i,n-1-i,j,n-1-j}) + 1;
	rep(i,n) rep(j,n){
		scanf("%d", &q); q--;
		x = q % n + 1; y = q / n + 1;
		used[x][y] = 1;
		ans += p[x][y];
		dfs(x, y);
	}
	printf("%d\n", ans);
}