#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define f(i, x, n) for (int i = x; i < (int)(n); ++i)

int const N = 500;
int n, x[N][N], dx[4] = { 0, 1, 0, -1 }, dy[4] = { -1, 0, 1, 0 };
bool em[N][N];

void go(int a, int b, int c){
	f(i, 0, 4){
		int na = a + dx[i], nb = b + dy[i];
		if (na == -1 || na == n || nb == -1 || nb == n || x[na][nb] <= c)continue;
		x[na][nb] = c;
		go(na, nb, c + !em[na][nb]);
	}
}

int main(){
	scanf("%d", &n);
	f(i, 0, n)f(j, 0, n)x[i][j] = min(min(i, n - 1 - i), min(j, n - 1 - j));
	int an = 0;
	f(i, 0, n * n){
		int t;
		scanf("%d", &t);
		--t;
		int a = t / n, b = t % n;
		an += x[a][b];
		em[a][b] = true;
		go(a, b, x[a][b]);
	}
	printf("%d\n", an);
}