// #define _GLIBCXX_DEBUG // for STL debug (optional)
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <string>
#include <cstring>
#include <deque>
#include <list>
#include <queue>
#include <stack>
#include <vector>
#include <utility>
#include <algorithm>
#include <map>
#include <set>
#include <complex>
#include <cmath>
#include <limits>
#include <cfloat>
#include <climits>
#include <ctime>
#include <cassert>
#include <numeric>
#include <fstream>
#include <functional>
#include <bitset>
#include <random>
using namespace std;
using ll = long long int;
using int64 = long long int;
 
template<typename T> void chmax(T &a, T b) {a = max(a, b);}
template<typename T> void chmin(T &a, T b) {a = min(a, b);}
template<typename T> void chadd(T &a, T b) {a = a + b;}
 
int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
const int INF = 1LL << 29;
const ll LONGINF = 1LL << 60;
const ll MOD = 1000000007LL;

/*
void solve_naive() {
    int N = rnd.NextInt(10, 15);
    
    int HW = N*N, ans = 0;
    vector<int> P(HW);
    iota(P.begin(), P.end(), 1);
    shuffle(P.begin(), P.end(), rnd.eng);
    
    vector< vector<bool> > dead(N, vector<bool>(N));
    for(int t=0; t<HW; t++) {
        // int v; cin >> v; v--;
        int v = P[t]; v--;
        int r = v/N, c = v%N;

        vector< vector<int> > dist(N, vector<int>(N, INF));
        queue< pair<int, int> > que;
        for(int i=0; i<N; i++) {
            dist[0][i] = 0;
            que.emplace(0, i);
            dist[i][0] = 0;
            que.emplace(i, 0);
            dist[i][N-1] = 0;
            que.emplace(i, N-1);
            dist[N-1][i] = 0;
            que.emplace(N-1, i);
        }

        while(que.size()) {
            int x, y; tie(x, y) = que.front(); que.pop();
            for(int k=0; k<4; k++) {
                int nx = x + dx[k], ny = y + dy[k];
                if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;
                int c = !dead[x][y];
                if(dist[nx][ny] > dist[x][y] + c) {
                    dist[nx][ny] = dist[x][y] + c;
                    que.emplace(nx, ny);
                }
            }
        }

        fprintf(stderr, "#debug (t = %d, r = %d, c = %d)\n", t+1, r+1, c+1);
        for(int i=0; i<N; i++) {
            for(int j=0; j<N; j++) {
                if(dead[i][j]) fprintf(stderr, "x ");
                else fprintf(stderr, "%d ", dist[i][j]);
            }
            fprintf(stderr, "\n");
        }
        ans += dist[r][c];
        dead[r][c] = true;
    }
    cout << ans << endl;
}
*/

void solve() {
    int N; cin >> N;
    vector<int> P(N*N);
    for(int i=0; i<N*N; i++) cin >> P[i], P[i]--;

    queue< pair<int, int> > que;
    vector< vector<int> > dist(N, vector<int>(N, INF));
    vector< vector<bool> > dead(N, vector<bool>(N));
    for(int i=0; i<N; i++) {
        dist[0][i] = 0; que.emplace(0, i);
        dist[i][0] = 0; que.emplace(i, 0);
        dist[N-1][i] = 0; que.emplace(N-1, i);
        dist[i][N-1] = 0; que.emplace(i, N-1);
    }

    while(que.size()) {
        int x, y; tie(x, y) = que.front(); que.pop();
        for(int k=0; k<4; k++) {
            int nx = x + dx[k], ny = y + dy[k];
            if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;
            int c = !dead[x][y];
            if(dist[nx][ny] > dist[x][y] + c) {
                dist[nx][ny] = dist[x][y] + c;
                que.emplace(nx, ny);
            }
        }
    }
    
    int ans = 0;
    for(int q=0; q<N*N; q++) {
        int r = P[q] / N, c = P[q] % N;
        ans += dist[r][c];
        dead[r][c] = true;

        que.emplace(r, c);
        while(que.size()) {
            int x, y; tie(x, y) = que.front(); que.pop();
            int c = !dead[x][y];
            for(int k=0; k<4; k++) {
                int nx = x + dx[k], ny = y + dy[k];
                if(nx < 0 or nx >= N or ny < 0 or ny >= N) continue;
                if(dist[nx][ny] > dist[x][y] + c) {
                    dist[nx][ny] = dist[x][y] + c;
                    que.emplace(nx, ny);
                }
            }
        }
    }
    cout << ans << endl;
}

int main() {
    // solve_naive();
    solve();
    return 0;
}


Measured memory_bytes: 5352