#include"bits/stdc++.h"
using namespace std;
#define rep(i,n) for(int i=0;i<n;i++)
#define int long long
typedef pair<int, int> P;
int p[250005];
int dis[505][505], cst[505][505];
int dx[] = { 1,0,-1,0 }, dy[] = { 0,1,0,-1 };

signed main() {
	int n; cin >> n;
	rep(i, n*n) {
		cin >> p[i]; p[i]--;
	}
	rep(i, n) {
		rep(j, n) {
			dis[i][j] = min(min(i, n - i - 1), min(j, n - j - 1));
			cst[i][j] = 1;
		}
	}

	int ans = 0;

	rep(i, n*n) {
		int ni = p[i] / n, nj = p[i] % n;
		ans += dis[ni][nj]; cst[ni][nj] = 0;
		queue<P>Q; Q.push(P(ni, nj));
		while (Q.size()) {
			P p = Q.front(); Q.pop();
			rep(j, 4) {
				int mi = p.first + dx[j], mj = p.second + dy[j];
				if (0 <= mi && mi < n && 0 <= mj && mj<n&&dis[mi][mj]>dis[p.first][p.second] + cst[p.first][p.second]) {
					dis[mi][mj] = dis[p.first][p.second] + cst[p.first][p.second];
					Q.push(P(mi, mj));
				}
			}
		}
	}
	cout << ans << endl;
}