#include <bits/stdc++.h>

using namespace std;

const int MX = 500;

int dist[MX][MX], a[MX][MX];

int main() {
	int n;
	ignore = scanf("%d", &n);
	
	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));
			a[i][j] = 1;
		}
	
	int ans = 0;
	for (int i = 0, p; i < n * n; i++) {
		ignore = scanf("%d", &p);
		
		int x = (p - 1) / n;
		int y = (p - 1) % n;
		
		ans += dist[x][y];
		a[x][y] = 0;
		
		vector<pair<int, int>> queue;
		queue.emplace_back(x, y);
		for (size_t j = 0; j < queue.size(); j++) {
			tie(x, y) = queue[j];
			
			for (int dx = -1; dx <= 1; dx++)
				for (int dy = -1; dy <= 1; dy++) {
					if (abs(dx) + abs(dy) != 1) continue;
					
					int xx = x + dx, yy = y + dy;
					if (xx < 0 || xx >= n || yy < 0 || yy >= n) continue;
					if (dist[xx][yy] > dist[x][y] + a[x][y]) {
						dist[xx][yy] = dist[x][y] + a[x][y];
						queue.emplace_back(xx, yy);
					}
				}
		}
	}
	
	printf("%d\n", ans);
	
	return 0;
}
