#include <iostream>
#include <cstdio>

using namespace std;

typedef long long ll;

ll n, ans;
bool v[505][505];
ll a[505][505];
ll dx[4] = {0, 1, 0, -1};
ll dy[4] = {1, 0, -1, 0};

void f(ll p, ll q)
{
    if(p < 1 || p > n) return;
    if(q < 1 || q > n) return;
    for(ll i = 0; i < 4; i++)
    {
        if(a[p + dx[i]][q + dy[i]] > a[p][q] + v[p][q])
        {
            a[p + dx[i]][q + dy[i]] = a[p][q] + v[p][q];
            f(p + dx[i], q + dy[i]);
        }
    }
}

int main()
{
    scanf("%lld", &n);
    for(ll i = 1; i <= n; i++)
    {
        for(ll j = 1; j <= n; j++)
        {
            v[i][j] = true;
            a[i][j] = min(min(i - 1, n - i), min(j - 1, n - j));
        }
    }
    for(ll i = 0; i < n * n; i++)
    {
        ll t;
        scanf("%lld", &t);
        ll x, y;
        t--;
        x = t % n + 1, y = t / n + 1;
        ans += a[x][y];
        v[x][y] = false;
        for(ll j = 0; j < 4; j++)
        {
            a[x][y] = min(a[x][y], a[x + dx[j]][y + dy[j]] + v[x + dx[j]][y + dy[j]]);
        }
        f(x, y);
    }
    printf("%lld\n", ans);
    return 0;
}
