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

const int Maxn = 505;
const int Maxm = 255;
const int Maxd = 4;
const int dy[Maxd] = {-1, 0, 1, 0};
const int dx[Maxd] = {0, -1, 0, 1};

struct pos {
    int a, b, c;
    pos(int a = 0, int b = 0, int c = 0): a(a), b(b), c(c) {}
};

bool dp[Maxn][Maxn][Maxm];
int n;
bool fr[Maxn][Maxn];
int a[Maxn * Maxn];
pos seq[Maxn * Maxn * Maxm];
int slen;
int res;

void Gen(int r, int c, int lvl)
{
    if (dp[r][c][lvl]) return;
    dp[r][c][lvl] = true;
    slen = 0;
    seq[slen++] = pos(r, c, lvl);
    for (int i = 0; i < slen; i++) {
        pos p = seq[i];
        for (int d = 0; d < Maxd; d++) {
            pos u = pos(p.a + dy[d], p.b + dx[d], p.c);
            if (0 <= u.a && u.a < n && 0 <= u.b && u.b < n) {
                if (!fr[u.a][u.b]) u.c++;
                if (u.c < Maxm && !dp[u.a][u.b][u.c]) {
                    dp[u.a][u.b][u.c] = true;
                    seq[slen++] = u;
                }
            }
        }
    }
}

int main()
{
    scanf("%d", &n);
    for (int i = 0; i < n * n; i++) {
        scanf("%d", &a[i]); a[i]--;
    }
    for (int i = 0; i < n; i++) {
        Gen(i, 0, 1);
        Gen(i, n - 1, 1);
        Gen(0, i, 1);
        Gen(n - 1, i, 1);
    }
    for (int i = 0; i < n * n; i++) {
        int r = a[i] / n, c = a[i] % n;
        int my = 0;
        while (!dp[r][c][my]) my++;
        my--;
        fr[r][c] = true;
        res += my;
        Gen(r, c, my);
    }
    cout << res << endl;
    return 0;
}
