#include <iostream>
#include <vector>
#include <string>
#include <queue>
using namespace std;

using cat = long long;

int main() {
	cin.sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int N;
	cin >> N;
	vector< vector<int> > dist(N, vector<int>(N, N+1));
	for(int i = 0; i < N; i++)
		for(int j = 0; j < N; j++)
			dist[i][j] = min(min(i, N-1-i), min(j, N-1-j));
	vector< vector<char> > cost(N, vector<char>(N, 1));
	cat ans = 0;
	for(int i = 0; i < N*N; i++) {
		int a;
		cin >> a;
		a--;
		int x = a/N, y = a%N;
		cost[x][y] = 0;
		ans += dist[x][y];
		static queue< pair<int, int> > q;
		q.push({x, y});
		while(!q.empty()) {
			x = q.front().first, y = q.front().second;
			q.pop();
			int d = dist[x][y] + cost[x][y];
			static constexpr int dx[] = {1, -1, 0, 0};
			static constexpr int dy[] = {0, 0, 1, -1};
			for(int k = 0; k < 4; k++) {
				if(dx[k]+x < 0 || dy[k]+y < 0 || dx[k]+x == N || dy[k]+y == N) continue;
				int xn = x + dx[k], yn = y + dy[k];
				if(d >= dist[xn][yn]) continue;
				dist[xn][yn] = d;
				q.push({xn, yn});
			}
		}
	}
	cout << ans << "\n";
}
