#include <bits/stdc++.h>
using namespace std;
#define ll long long

vector<vector<int>> dist;
vector<vector<bool>> vacant;
int N;
ll ans = 0;
vector<int> dx = {0,0,1,-1}, dy = {1,-1,0,0};

void dfs(int x, int y) {
    for (int i = 0; i <= 3; i++) {
        int nx = x+dx[i], ny = y+dy[i];
        if (nx <= -1 || nx >= N || ny <= -1 || ny >= N) continue;
        int ndist = dist[x][y];
        if (!vacant[x][y]) ndist++;
        if (ndist < dist[nx][ny]) {
            dist[nx][ny] = ndist;
            dfs(nx, ny);
        }
    }
    return;
}

int main() {
    cin >> N;
    
    vector<int> P(N*N);
    for (int i = 0; i < N*N; i++) {
        cin >> P[i];
        P[i]--;
    }
    
    dist = vector<vector<int>>(N, vector<int>(N, 0));
    vacant = vector<vector<bool>>(N, vector<bool>(N, false));
    
    for (int i = 0; i < N; i++) {
        for (int j = 0; j < N; j++) {
            dist[i][j] = min(min(i, N-1-i), min(j, N-1-j));
        }
    }
    
    for (int i = 0; i < N*N; i++) {
        int x = P[i]/N, y = P[i]%N;
        ans += dist[x][y];
        vacant[x][y] = true;
        dfs(x, y);
    }
    cout << ans << endl;
    return 0;
}
