#pragma GCC optimize("O2,unroll-loops")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 506;
int mat[maxn][maxn], pres[maxn][maxn], n;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void rem(int x, int y) {
	queue<pair<int, int>> a;
	pres[x][y] = 0;
	a.push({x, y});
	mat[x][y]--;
	while(!a.empty()) {
		int x, y;
		tie(x, y) = a.front();
		a.pop();
		for(int i = 0; i < 4; i++) {
			int tx = x + dx[i], ty = y + dy[i];
			if(tx>=0&&tx<=n&&ty>=0&&ty<=n&&mat[tx][ty]>mat[x][y]+pres[tx][ty]) {
				mat[tx][ty] = mat[x][y]+pres[tx][ty];
				a.push({tx, ty});
			}
		}
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;++n;
	for(int i = 1; i < n; i++)
		for(int j = 1; j < n; j++) {
			mat[i][j] = min({i, n-i, j, n-j});
			pres[i][j] = 1;
		}
	ll ans = 0;
	for(int v, i = 0; i < (n-1)*(n-1); i++) {
		cin >> v;
		ans += mat[1 + (v-1)/(n-1)][1 + (v-1)%(n-1)] - 1;
		rem(1 + (v-1)/(n-1), 1 + (v-1)%(n-1));
		/**
		cout << 1 + (v-1)/(n-1) << " " << 1 + (v-1)%(n-1) << "=======\n";
		for(int i = 0; i <= n; i++, cout << '\n')
			for(int j = 0; j <= n; j++)
				cout << mat[i][j] << " ";
		cout << ans << "=======\n";
		***/
	}
	cout << ans;
}
#pragma GCC optimize("O2,unroll-loops")
#pragma GCC target("avx,sse,tune=native")
#include<bits/stdc++.h>
#define all(x) x.begin(), x.end()
using namespace std;
using vi = vector<int>;
using ll = long long;
const int maxn = 506;
int mat[maxn][maxn], pres[maxn][maxn], n;
int dx[] = {1, -1, 0, 0}, dy[] = {0, 0, 1, -1};
void rem(int x, int y) {
	queue<pair<int, int>> a;
	pres[x][y] = 0;
	a.push({x, y});
	mat[x][y]--;
	while(!a.empty()) {
		int x, y;
		tie(x, y) = a.front();
		a.pop();
		for(int i = 0; i < 4; i++) {
			int tx = x + dx[i], ty = y + dy[i];
			if(tx>=0&&tx<=n&&ty>=0&&ty<=n&&mat[tx][ty]>mat[x][y]+pres[tx][ty]) {
				mat[tx][ty] = mat[x][y]+pres[tx][ty];
				a.push({tx, ty});
			}
		}
	}
}
int main() {
	cin.tie(0)->sync_with_stdio(0);
	cin >> n;++n;
	for(int i = 1; i < n; i++)
		for(int j = 1; j < n; j++) {
			mat[i][j] = min({i, n-i, j, n-j});
			pres[i][j] = 1;
		}
	ll ans = 0;
	for(int v, i = 0; i < (n-1)*(n-1); i++) {
		cin >> v;
		ans += mat[1 + (v-1)/(n-1)][1 + (v-1)%(n-1)] - 1;
		rem(1 + (v-1)/(n-1), 1 + (v-1)%(n-1));
		/**
		cout << 1 + (v-1)/(n-1) << " " << 1 + (v-1)%(n-1) << "=======\n";
		for(int i = 0; i <= n; i++, cout << '\n')
			for(int j = 0; j <= n; j++)
				cout << mat[i][j] << " ";
		cout << ans << "=======\n";
		***/
	}
	cout << ans;
}
