#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i = (a); i < (b); ++i)
#define rrep(i,a,b) for(int i = (b); i --> (b);)
#define trav(x,v) for(auto &x : (v))
#define all(v) (v).begin(),(v).end()
#define sz(v) (int)(v).size()
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef long long ll;

const int dx[] = {0,0,1,-1};
const int dy[] = {1,-1,0,0};

int main(){
	cin.sync_with_stdio(0); cin.tie(0);
	cin.exceptions(cin.failbit);

	int n;
	cin >> n;

	vector<vi> cost(n, vi(n));
	rep(x,0,n) rep(y,0,n)
		cost[x][y] = 1 + min( min(x, n-1-x),
				  min(y, n-1-y) );

	vector<vector<bool>> cinema(n, vector<bool>(n, true));

	function<void(int,int,int)> upd = [&](int x, int y, int d){
		cost[x][y] = d;
		rep(t,0,4){
			int x1 = x + dx[t], y1 = y + dy[t];
			if(max(x1, y1) < n && min(x1,y1) >= 0){
				int cnd = d + cinema[x1][y1];
				if(cost[x1][y1] > cnd) upd(x1, y1, cnd);
			}
		}
	};

	int ans = 0;

	rep(_,0,n*n){
		int p;
		cin >> p;
		--p;
		int x = p / n, y = p % n;
		ans += cost[x][y]-1;
		upd(x, y, cost[x][y] - 1);
		cinema[x][y] = false;
	}

	cout << ans << endl;
}
