#include <bits/stdc++.h>
#define be(v) (v).begin(),(v).end()
#define pb(q) push_back(q)
typedef long long ll;
using namespace std;
const ll mod=1000000007, INF=mod*mod*3LL;
#define doublecout(a) cout<<fixed<<setprecision(10)<<a<<endl;
const int N = 575;
int n;
int w[N][N];
int dis[N][N];
int dx[4] = {0, -1, 1, 0};
int dy[4] = {-1, 0, 0, 1};

void dfs(int y, int x){
	int d = dis[y][x] + w[y][x];
	for(int k=0;k<4;k++){
		int i = y + dy[k], j = x + dx[k];
		if(i < 0 || i >= n || j < 0 || j >= n) continue;
		if(dis[i][j] > d){
			dis[i][j] = d;
			dfs(i, j);
		}
	}
}
int main() {
    cin.tie(0);
    cout.tie(0);
    ios::sync_with_stdio(false);
    int ans = 0, p;
    cin >> n;
    for(int i=0;i<n;i++)for(int j=0;j<n;j++){
    	dis[i][j] = min({i, j, n-i-1, n-j-1});
    	w[i][j] = 1;
    }
    int m = n * n;
    for(int i=0;i<m;i++){
    	cin >> p; p--;
    	int y = p / n;
    	int x = p % n;
    	ans += dis[y][x];
    	w[y][x] = 0;
    	dfs(y, x);
    }
    cout << ans << endl;
    return 0;
}
