#include <bits/stdc++.h>

using namespace std;

int n, ans, p[250005], d[505][505], mx[] = {0, 1, 0, -1}, my[] = {1, 0, -1, 0}, vis[505][505];

int dis(int a, int b, int c, int d)
{
    return abs(a - c) + abs(b - d);
}

void dfs(int x, int y, int p, int q, int now)
{
    if (x < 1 || x > n || y < 1 || y > n) return ;
    if (d[x][y] <= d[p][q] + now) return ;
    d[x][y] = d[p][q] + now;
    now += 1 - vis[x][y];
    for (int i = 0; i < 4; i++)
        dfs(x + mx[i], y + my[i], p, q, now);
}

int main()
{
    cin >> n;
    for (int i = 1; i <= n * n; i++) cin >> p[i];
    for (int i = 1; i <= n; i++) for (int j = 1; j <= n; j++) 
        d[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
    for (int i = 1; i <= n * n; i++)
    {
        int x = p[i] / n + 1;
        int y = p[i] % n;
        if (y == 0)
        {
            x--;
            y = n;
        }
        vis[x][y] = 1;
        ans += d[x][y];
        for (int j = 0; j < 4; j++) dfs(x + mx[j], y + my[j], x, y, 0);
    }
    cout << ans << endl;
    return 0;
}