#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <map>
#include <algorithm>

using namespace std; 

int n, dx[4] = {-1, 0, 0, 1}, dy[4] = {0, 1, -1, 0}; 
int dis[505][505], vis[505][505]; 

void dfs(int x, int y) {
    for (int i = 0; i < 4; i++) {
        int nx = x + dx[i], ny = y + dy[i]; 
        if (dis[nx][ny] > dis[x][y] + 1 - vis[nx][ny]) {
            dis[nx][ny] = dis[x][y] + 1 - vis[nx][ny];
            dfs(nx, ny);
        }
    }
}

int main() {
    scanf("%d", &n); 
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            dis[i][j] = min(min(i, j), min(n - i + 1, n - j + 1)); 
        }
    }
    int ans = 0; 
    for (int i = 1; i <= n * n; i++) {
        int p;
        scanf("%d", &p); 
         
        int x = (p - 1) / n + 1, y = p - (x - 1) * n;
        vis[x][y] = 1;
        ans += dis[x][y] - 1; 
        for (int j = 0; j < 4; j++) {
            int nx = x + dx[j], ny = y + dy[j]; 
            dis[x][y] = min(dis[x][y], dis[nx][ny]); 
        }
        dfs(x, y); 
    }
    printf("%d\n", ans); 
    return 0; 
}