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

#define fi first
#define se second

const int N = 511 * 511;

int a[N];
bool vis[N];

using pii = pair <int, int>;
pii dir[] = {{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

int main() {
	ios :: sync_with_stdio(0);
	int n; cin >> n;
	
	for(int i = 0; i < n; i ++)
		for(int j = 0; j < n; j ++)
			a[i * n + j] = min(min(i, n - i - 1),
				min(j, n - j - 1));
	
	int ans = 0;
	for(int i = 0; i < n * n; i ++) {
		int id; cin >> id; id --;
		ans += a[id];
		vis[id] = 1;
		
		queue <int> q; q.push(id);
		while(!q.empty()) {
			int x = q.front(); q.pop();
			for(auto p : dir) {
				int tx = x / n + p.fi;
				int ty = x % n + p.se;
				if(tx >= 0 && tx < n && ty >= 0 && ty < n)
					if(a[tx * n + ty] > a[x] + 1 - vis[x]) {
						a[tx * n + ty] = a[x] + 1 - vis[x];
						q.push(tx * n + ty);
					}
			}
		}
		
		//~ cout << id << '\n';
		//~ for(int i = 0; i < n; i ++) {
			//~ for(int j = 0; j < n; j ++)
				//~ cout << a[i * n + j] << ' ';
			//~ cout << '\n';
		//~ }
	}
	cout << ans << '\n';
}
