#include<bits/stdc++.h>
#define pb emplace_back
using namespace std;
using ll = long long;
const int maxn = 510;
int n, dis[maxn][maxn], od[maxn][maxn];
int p[maxn];
int res;
bool ok[maxn][maxn];
void get_ans(int x, int y) {

	res += dis[x][y];
	queue<pair<int,int>> q;
	int val = dis[x][y];
	ok[x][y] = true;
	q.push({x, y});
	while (!q.empty()) {
		auto [x, y] = q.front();
		q.pop();
		for (int i : {-1, 1}) {
			int val = dis[x][y] + !ok[x][y];
			if (val < dis[x+i][y]) {
				dis[x+i][y] = val;
				q.push({x+i, y});
			}
			if (val < dis[x][y+i]) {
				dis[x][y+i] = val;
				q.push({x, y+i});
			}
		}
	}
}
signed main(){
	ios_base::sync_with_stdio(0), cin.tie(0);
	cin >> n;
	for (int i = 1;i <= n;++i)
		for (int j = 1;j <= n;++j)
			dis[i][j] = min({i-1, n-i, j-1, n-j});

	for (int i = 1;i <= n * n;++i) {
		cin >> p[i];
		--p[i];
		int r = p[i] / n, c = p[i] % n;
		get_ans(r+1, c+1);
	}
	cout << res << '\n';
}
