//WA => CARO!
#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define pb push_back 
#define fastcin ios::sync_with_stdio(false); cin.tie(NULL);
#define endl '\n'
#define MAX 505

int n;
int h[MAX][MAX];
int oc[MAX][MAX];

int x[4] = {0, 0, 1, -1};
int y[4] = {1, -1, 0, 0};

void dfs(int l, int c){
	for (int i=0; i<4; i++){
		if (l+x[i] < 0 or l+x[i] >= n or c+y[i] < 0 or c+y[i] >= n)
			continue;
		if (h[l+x[i]][c+y[i]] > h[l][c]+oc[l][c]){
			h[l+x[i]][c+y[i]] = h[l][c]+oc[l][c];
			dfs(l+x[i], c+y[i]);
		}
	}
}

tuple<int, int> coord(int k){
	k--;
	return {k/n, k%n};
}

signed main(){
	cin >> n;
	for (int i=0; i<n; i++){
		for (int j=0; j<n; j++){
			h[i][j]=min({i, j, n-1-i, n-1-j});
			oc[i][j]=1;
		}
	}
	vector<int> p(n*n);
	for (auto &x: p) cin >> x;
	int sum = 0;
	for (int i=0; i<n*n; i++){
		int l, c;
		tie(l, c) = coord(p[i]);
		oc[l][c] = 0;
		sum+=h[l][c];
		dfs(l, c);
	}
	cout << sum << endl;
}
