#include <bits/stdc++.h>
using namespace std;

const int MAXN = 505;
const int MAXM = 505 * 505;

int n, p[MAXM], d[MAXN][MAXN];
bool v[MAXN][MAXN];
queue < pair <int, int> > q;

int di[] = {1, -1, 0, 0};
int dj[] = {0, 0, 1, -1};

int main(){
	scanf("%d", &n);
	
	for(int i = 0; i < n; i++)
	for(int j = 0; j < n; j++){
		d[i][j] = min(min(i + 1, n - i), min(j + 1, n - j));
		v[i][j] = true;
	}
	
	long long res = 0;
	for(int i = 0; i < n * n; i++){
		scanf("%d", &p[i]);
		p[i]--;
		
		int x = p[i] / n;
		int y = p[i] % n;
		
		d[x][y]--;
		v[x][y] = false;
		q.push(make_pair(x, y));
		res += d[x][y];
		
		while(!q.empty()){
			pair <int, int> t = q.front();
			q.pop();
			
			for(int dir = 0; dir < 4; dir++){
				int nx = t.first + di[dir];
				int ny = t.second + dj[dir];
				
				if(nx < 0 || nx >= n || ny < 0 || ny >= n)continue;
				
				if(d[t.first][t.second] + v[nx][ny] < d[nx][ny]){
					d[nx][ny]--;
					q.push(make_pair(nx, ny));
				}
			}
		}
	}
	printf("%lld\n", res);

	return 0;
}
