#include <stdio.h>
#include <stdlib.h>

#include <iostream>
#include <iomanip>
#include <sstream>
#include <fstream>
#include <stdint.h>
#include <string.h>

#define _USE_MATH_DEFINES
#include <math.h>

#include <vector>
#include <list>
#include <set>
#include <map>
#include <unordered_map>
#include <unordered_set>
#include <queue>
#include <stack>
#include <deque>
#include <string>

#include <algorithm>
#include <functional>
#include <bitset>
#include <functional>
#include <chrono>
#include <random>

#define sqr(x) (x) * (x)

typedef unsigned int u32;
typedef int i32;
typedef unsigned long long int u64;
typedef long long int i64;
typedef uint16_t u16;
typedef int16_t i16;
typedef uint8_t u8;
typedef int8_t i8;

using namespace std;
using namespace std::chrono;

const i64 mod = 1'000'000'000ll + 7;
//const i64 smod = 998244353ll;
//const i64 inf = 1'000'000'000'000'000'000ll + 7;
//const i64 inf = 1'000'000'007;

const long double eps = 1e-8;

const i64 MAXN = 300'000 + 6;
const i64 MAXM = 502;

i64 n;
i64 a[MAXM][MAXM];
i64 b[MAXM][MAXM];
i64 c[MAXM][MAXM];
i64 u[MAXM][MAXM];

i64 p[MAXN];

void init_a() {
    i64 c = 1;
    for (i64 i = 1; i <= n; i++) {
        for (i64 j = 1; j <= n; j++) {
            a[i][j] = c++;
        }
    }
}

void init_b() {
    for (i64 k = 1; k <= n / 2 + n % 2; k++) {
        i64 l = k;
        i64 r = n + 1 - k;

        for (i64 i = l; i <= r; i++) {
            b[i][l] = k - 1;
            b[i][r] = k - 1;
        }

        for (i64 j = l; j <= r; j++) {
            b[l][j] = k - 1;
            b[r][j] = k - 1;
        }
    }
}

void out(i64 a[MAXM][MAXM]) {
    stringstream ss;
    for (i64 i = 1; i <= n; i++) {
        for (i64 j = 1; j <= n; j++) {
            ss << a[i][j] << " ";
        }
        ss << endl;
    }
    cerr << ss.str();
}

//i64 bfs(i64 i, i64 j) {
//    memset(c, 127, MAXM * MAXM * sizeof(b[0][0]));
//
//    vector<pair<i64, i64>> q;
//
//    set<pair<i64, i64>> u;
//    u.insert({i, j});
//    q.push_back({ i, j });
//    c[i][j] = 0;
//
//    for (i64 tt = 0; tt < q.size(); tt++) {
//        auto 
//    }
//}

const vector<pair<i64, i64>> z = {
    {-1, 0},
    {1, 0},
    {0, -1},
    {0, 1}
};


void upd(i64 tt, i64 si, i64 sj) {
    if (a[si][sj] != p[tt]) {
        throw 1;
    }

    a[si][sj] = 0;

    vector<pair<i64, i64>> q;
    q.push_back({ si, sj });
    u[si][sj] = tt;

    //for (auto t : z) {
    //    i64 ni = si + t.first;
    //    i64 nj = sj + t.second;

    //    b[si][sj] = min(b[si][sj], b[ni][nj]);
    //}


    for (i64 k = 0; k < q.size(); k++) {
        i64 ci = q[k].first;
        i64 cj = q[k].second;

        i64 v = b[ci][cj] + (a[ci][cj] ? 1 : 0);
        for (auto t : z) {
            i64 ni = ci + t.first;
            i64 nj = cj + t.second;

            if (u[ni][nj] == tt) {
                continue;
            }

            if (b[ni][nj] <= v) {
                continue;
            }

            b[ni][nj] = v;
            u[ni][nj] = tt;
            q.push_back({ ni, nj });
        }
    }
}

void brute() {
    i64 s = 0;
    for (i64 i = 1; i <= n; i++) {
        for (i64 j = 1; j <= n; j++) {
            s += b[i][j];
        }
    }
    cerr << s << endl;
}

int main(int argc, char* argv[]) {
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0); cout.precision(15); cout.setf(ios::fixed);

    cin >> n;
    for (i64 tt = 1; tt <= n * n; tt++) {
        cin >> p[tt];
    }

    init_a();
    init_b();
    i64 R = 0;
    for (i64 tt = 1; tt <= n * n; tt++) {
        i64 i = (p[tt] - 1) / n + 1;
        i64 j = (p[tt] - 1) % n + 1;

        //cerr << i << " " << j << endl;
        //out(b);
        //cerr << "---" << endl;

        R += b[i][j];
        upd(tt, i, j);
    }
    cout << R << endl;
    return 0;
}

Measured memory_bytes: 18788