/* In the name of Allah */
#include<bits/stdc++.h>
using namespace std;

const int N = 500 + 5;
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};
int n, id[N * N], num[N][N], dist[N][N];
bool mark[N][N];

bool is_valid(int x, int y) {
	return ~x && ~y && x < n && y < n;
}

void dfs(int x, int y) {
	for (int i = 0; i < 4; i++) {
		int u = x + dx[i], v = y + dy[i];
		if (is_valid(u, v) && dist[u][v] > dist[x][y] + !mark[u][v])
			dist[u][v] = dist[x][y] + !mark[u][v], dfs(u, v);
	}
}

void read_input() {
	cin >> n;
	for (int i = 0; i < n * n; i++) {
		int x;
		cin >> x;
		id[i] = --x;
		num[x / n][x % n] = i;
	}
}

void solve() {
	int ans = 0;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			dist[i][j] = min(min(i, j) + 1, n - max(i, j));
	for (int i = 0; i < n * n; i++) {
		int x = id[i] / n, y = id[i] % n, mn = dist[x][y];
		for (int j = 0; j < 4; j++) {
			int u = x + dx[j], v = y + dy[j];
			mn = min(mn, is_valid(u, v)? dist[u][v]: 0);
		}
		mark[x][y] = true, ans += dist[x][y] = mn, dfs(x, y);
	}
	cout << ans;
}

int main() {
	ios:: sync_with_stdio(0), cin.tie(0), cout.tie(0);
	read_input(), solve();
	return 0;
}
