#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};

int dp[Maxn][Maxn];
int n;
bool fr[Maxn][Maxn];
int a[Maxn * Maxn];
int res;

void Traverse(int r, int c, int bef)
{
    if (r < 0 || r >= n || c < 0 || c >= n) return;
    if (!fr[r][c]) bef++;
    if (bef >= dp[r][c]) return;
    dp[r][c] = bef;
    for (int d = 0; d < Maxd; d++)
        Traverse(r + dy[d], c + dx[d], bef);
}

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