#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
#ifdef TEST
#define getchar_unlocked _getchar_nolock
#endif
#define int long long
#define ins insert
#define mp make_pair
#define mt make_tuple
#define pb push_back
#define pf push_front
#define pp pop
#define ppb pop_back
#define ppf pop_front
#define eb emplace_back
#define g0(a) get<0>(a)
#define g1(a) get<1>(a)
#define g2(a) get<2>(a)
#define g3(a) get<3>(a)
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
typedef double db;
typedef long long ll;
typedef pair<int, int> ii;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tree<ii, null_type, less<ii>, rb_tree_tag, tree_order_statistics_node_update> ordered_set;

int N, ans, P[505], dist[505][505], dr[] = {-1, 0, 0, 1}, dc[] = {0, -1, 1, 0};
bool grid[505][505];

void calc() {
	int d, r, c;
	deque<iii> D;
	for (int i = 2; i < N; i++)
		for (int j = 2; j < N; j++)
			dist[i][j] = 1e9;
	for (int i = 1; i <= N; i++)
		D.emplace_front(grid[1][i], 1, i), dist[1][i] = grid[1][i];
	for (int i = 2; i <= N; i++)
		D.emplace_front(grid[i][1], i, 1), dist[i][1] = grid[i][1];
	for (int i = 2; i <= N; i++)
		D.emplace_front(grid[N][i], N, i), dist[N][i] = grid[N][i];
	for (int i = 2; i < N; i++)
		D.emplace_front(grid[i][N], i, N), dist[i][N] = grid[i][N];
	while (!D.empty()) {
		tie(d, r, c) = D.front();
		D.ppf();
		if (dist[r][c] < d) continue;
		for (int k = 0; k < 4; k++)
			if (1 <= r + dr[k] && r + dr[k] <= N && 1 <= c + dc[k] && c + dc[k] <= N && dist[r + dr[k]][c + dc[k]] > d + grid[r + dr[k]][c + dc[k]]) {
				dist[r + dr[k]][c + dc[k]] = d + grid[r + dr[k]][c + dc[k]];
				if (grid[r + dr[k]][c + dc[k]]) D.emplace_back(dist[r + dr[k]][c + dc[k]], r + dr[k], c + dc[k]);
				else D.emplace_front(dist[r + dr[k]][c + dc[k]], r + dr[k], c + dc[k]);
			}
	}
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++)
			dist[i][j] -= grid[i][j];
}

main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cin >> N;
	for (int i = 1; i <= N; i++)
		for (int j = 1; j <= N; j++) grid[i][j] = 1;
	calc();
	for (int i = 1, P; i <= N * N; i++) {
		cin >> P;
		ans += dist[(P - 1) / N + 1][(P - 1) % N + 1];
		grid[(P - 1) / N + 1][(P - 1) % N + 1] = 0;
		calc();
	}
	cout << ans << '\n';
}
