#include<algorithm>
#include<bitset>
#include<cassert>
#include<cfloat>
#include<climits>
#include<cmath>
#include<deque>
#include<functional>
#include<iomanip>
#include<iostream>
#include<map>
#include<numeric>
#include<queue>
#include<set>
#include<stack>
#include<string>
#include<unordered_map>
#include<unordered_set>
#include<utility>
#include<vector>

using namespace std;

using lint = long long;
using P = pair<int, int>;
using LLP = pair<long long, long long>;

#define REP(i, x, n) for(int i = (x), i##_len = (int)(n) ; i < i##_len ; ++i)
#define rep(i, n) for(int i = 0, i##_len = (int)(n) ; i < i##_len ; ++i)
#define reps(i, n) for(int i = 1, i##_len = (int)(n) ; i <= i##_len ; ++i)
#define rrep(i, n) for(int i = (int)(n) - 1 ; i >= 0 ; --i)
#define rreps(i, n) for(int i = (int)(n) ; i > 0 ; --i)
#define SORT(x) sort((x).begin(), (x).end())
#define SORT_INV(x) sort((x).rbegin(), (x).rend())
#define REVERSE(x) reverse((x).begin(), (x).end())
#define TWINS(x) cout << ((x) ? "Yay!" : ":(") << '\n'

constexpr int IINF = (1 << 30) - 1;
constexpr long long LLINF = 1LL << 61;
constexpr double EPS = 1e-10;

constexpr int dx4[] = {1, 0, -1, 0}, dy4[] = {0, 1, 0, -1};
constexpr int dx8[] = {1, 1, 0, -1, -1, -1, 0, 1}, dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template<typename T>
bool chmax(T& a, T b){
    if(a < b){
        a = b;
        return true;
    }
    return false;
}

template<typename T>
bool chmin(T& a, T b){
    if(b < a){
        a = b;
        return true;
    }
    return false;
}

struct node{
    int y, x, cost;
};

int main(){
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);

    int n;
    cin >> n;

    vector<int> ps(n * n);
    rep(i, n * n){
        cin >> ps[i];
        --ps[i];
    }

    vector< vector<int> > cost(n, vector<int>(n, IINF));
    deque<node> que;
    rep(i, n){
        cost[0][i] = 0;
        que.push_back({0, i, 0});
        cost[i][0] = 0;
        que.push_back({i, 0, 0});
        cost[n - 1][i] = 0;
        que.push_back({n - 1, i, 0});
        cost[i][n - 1] = 0;
        que.push_back({i, n - 1, 0});
    }

    auto inside = [&](int y, int x){
        return 0 <= y && y < n && 0 <= x && x < n;
    };

    while(!que.empty()){
        node now = que.front();
        que.pop_front();

        if(cost[now.y][now.x] < now.cost){
            continue;
        }

        rep(i, 4){
            int ny = now.y + dy4[i];
            int nx = now.x + dx4[i];
            if(inside(ny, nx) && chmin(cost[ny][nx], now.cost + 1)){
                que.push_back({ny, nx, now.cost + 1});
            }
        }
    }

    int ans = 0;
    vector< vector<bool> > iru(n, vector<bool>(n, true));

    for(auto p : ps){
        int y = p / n;
        int x = p % n;

        ans += cost[y][x];

        iru[y][x] = false;

        que.push_back({y, x, cost[y][x]});

        while(!que.empty()){
            node now = que.front();
            que.pop_front();

            if(cost[now.y][now.x] < now.cost){
                continue;
            }

            rep(i, 4){
                int ny = now.y + dy4[i];
                int nx = now.x + dx4[i];

                if(!inside(ny, nx)){
                    continue;
                }

                int nc = now.cost + iru[now.y][now.x];
                if(nc == now.cost && chmin(cost[ny][nx], nc)){
                    que.push_front({ny, nx, nc});
                }
                if(nc != now.cost && chmin(cost[ny][nx], nc)){
                    que.push_back({ny, nx, nc});
                }
            }
        }
    }

    cout << ans << endl;

    cout << flush;

    return 0;
}

Measured memory_bytes: 6652