//#pragma GCC optimize("O3")
//#pragma GCC target("avx2")
// 293206GT
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <set>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <unordered_set>
#include <stack>
#include <string>
#include<algorithm>
#include <random>
#include <bitset>
#include <chrono>

typedef long long ll;

using namespace std;
#define put(a) cout << (a) << '\n'
#define sqr(x) (x) * (x)
typedef pair<ll, ll> pii;
typedef long double ld;

typedef pair<ld, ld> pld;
#define rep(x, y, a) for (int x = (y); x < (int)(a); ++x)
#define all(a) a.begin(), a.end()
#define chkmax(a, b) a = max(a, (b))
#define chkmin(a, b) a = min(a, (b))
#define prev asasdasd
#define next aasdasda
#define left asdasdasdasd
#define right asnabsdkasdl
#define rank asdasdknlasd
#define move asdasdas

typedef vector<ll> vi;
typedef vector<pii> vpii;
namespace IO {
    template<class A, class B>
    ostream &operator<<(ostream &out, pair<A, B> &a) {
        out << a.first << " " << a.second;
        return out;
    }

    template<class A, class B>
    ostream &operator<<(ostream &out, vector<pair<A, B>> &a) {
        for (pair<A, B> x: a)
            out << x.first << " " << x.second << '\n';
    }

    template<class A>
    ostream &operator<<(ostream &out, vector<A> &a) {
        for (A x: a) out << x << ' ';
        return out;
    }

    template<class A, class B>
    istream &operator>>(istream &in, pair<A, B> &a) {
        in >> a.first >> a.second;
        return in;
    }

    template<class A>
    istream &operator>>(istream &in, vector<A> &a) {
        for (A &x: a) in >> x;
        return in;
    }
}
using namespace IO;
#define int ll
const int N = 3e5 + 4;
//const int N = 6;
int n;
int trans(int i, int j) {
    return i * n + j;
}
void solve() {
    cin >> n;
    vi p(n * n);
    cin >> p;
    vi dist(n * n, 1e10);
    vi neig = {1, -1, n, -n};
    vector<set<int>> back(n * n);
    rep(i, 0, n) rep(j, 0, n) {
        int cur_d = i;
        set<int> par = {-n};
        if (n - i - 1 < cur_d) {cur_d = n - i - 1;par.clear();}
        if (n - i - 1 == cur_d) {par.insert(n);}
        if (j < cur_d) {cur_d = j;par.clear();}
        if (j == cur_d) {par.insert(-1);}
        if (n - j - 1 < cur_d) {cur_d = n - j - 1;par.clear();}
        if (n - j - 1 == cur_d) {par.insert(1);}
        dist[trans(i, j)] = cur_d;
        back[trans(i, j)] = par;
    }
    int res = 0;
    vi used(n * n, 1);
    for (int i : p) {
        --i;
        res += dist[i];
        used[i] = 0;
        queue<int> q;
        q.push(i);
        while (q.size()) {
            int v = q.front();
            q.pop();
            for (int j: neig) {
                if (v + j < 0 || v + j >= n * n) continue;
                if (dist[v + j] > dist[v] + used[v]) {
                    dist[v + j] = dist[v] + used[v];
                    q.push(v + j);
                }
            }
        }
    }
    put(res);

}


signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cout.precision(30);
//    freopen("input.txt", "r", stdin);
//    freopen("input.txt", "r", stdin);
    int q = 1;
//    cin >> q;
    while (q--) solve();
}



Measured memory_bytes: 26232