#include <iostream>
#include <iomanip>
#include <vector>
#include <algorithm>
#include <numeric>
#include <functional>
#include <cmath>
#include <queue>
#include <stack>
#include <set>
#include <map>
#include <sstream>
#include <string>
#define _repargs(_1,_2,_3,name,...) name
#define _rep(i,n) repi(i,0,n)
#define repi(i,a,b) for(int i=(int)(a);i<(int)(b);++i)
#define rep(...) _repargs(__VA_ARGS__,repi,_rep,)(__VA_ARGS__)
#define all(x) (x).begin(),(x).end()
#define mod 1000000007
#define inf 2000000007
#define mp make_pair
#define pb push_back
typedef long long ll;
using namespace std;
template <typename T>
inline void output(T a, int p = 0) {
    if(p) cout << fixed << setprecision(p)  << a << "\n";
    else cout << a << "\n";
}
// end of template

struct cell {
    int U, D, R, L;
    bool is_same;
    int value() {
        return min({U, D, R, L});
    }
    void same() {
        int v = value();
        U = v;
        D = v;
        R = v;
        L = v;
    }
    void update() {
        int v = value() + 1;
        U = min(v, U);
        D = min(v, D);
        R = min(v, R);
        L = min(v, L);
    }
    bool valid() {
        int v = value();
        if(is_same) {
            if(U - v > 0 || D - v > 0 || R - v > 0 || L - v > 0) return false;
        }
        if(U - v > 1 || D - v > 1 || R - v > 1 || L - v > 1) return false;
        return true;
    }
};

int N;

// U, D, L, R
int dx[4] = {0, 0, -1, 1};
int dy[4] = {-1, 1, 0, 0};

bool is_in(int r, int c, int i) {
    return r + dy[i] >= 0 && r + dy[i] < N && c + dx[i] >= 0 && c + dx[i] < N;
}

void dfs(int r, int c, vector<vector<cell>> &A) {
    A[r][c].update();
    if(A[r][c].is_same) A[r][c].same();
    // U
    if(is_in(r, c, 0)) A[r + dy[0]][c + dx[0]].D = A[r][c].U;
    if(is_in(r, c, 1)) A[r + dy[1]][c + dx[1]].U = A[r][c].D;
    if(is_in(r, c, 2)) A[r + dy[2]][c + dx[2]].R = A[r][c].L;
    if(is_in(r, c, 3)) A[r + dy[3]][c + dx[3]].L = A[r][c].R;
    
    
    rep(i, 4) {
        if(r + dy[i] >= 0 && r + dy[i] < N && c + dx[i] >= 0 && c + dx[i] < N) {
            if(!A[r + dy[i]][c + dx[i]].valid()) {
                dfs(r + dy[i], c + dx[i], A);
            }
        }
    }
}

void debug(vector<vector<cell>> &A) {
    rep(i, N) {
        rep(j, N) {
            cout << " " << A[i][j].U << " *";
        }
        cout << endl;
        rep(j, N) {
            cout << A[i][j].L << " " << A[i][j].R << "*";
        }
        cout << endl;
        rep(j, N) {
            cout << " " << A[i][j].D << " *";
            }
        cout << endl;
        rep(j, N) cout << "****";
        cout << endl;
    }
    
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(0);
    // source code
    
    cin >> N;
    vector<int> B(N * N);
    rep(i, N * N) {
        cin >> B[i];
        B[i]--;
    }
    
    vector<vector<cell>> A(N, vector<cell>(N));
    rep(i, N) rep(j, N) {
        int d = min({i, j, N - 1 - i, N - 1 - j});
        A[i][j].U = min({d + 1, i});
        A[i][j].D = min({d + 1, N - 1 - i});
        A[i][j].L = min(d + 1, j);
        A[i][j].R = min(d + 1, N - 1 - j);
        A[i][j].is_same = false;
    }
    
    
    int sum = 0;
    rep(i, N * N) {
        int r = B[i] / N;
        int c = B[i] % N;
        sum += A[r][c].value();
//        cout << r << "," << c << ":" << A[r][c].value() << endl;
        A[r][c].same();
        A[r][c].is_same = true;
        dfs(r, c, A);
//        debug(A);
    }
    output(sum);
    
    return 0;
}


Measured memory_bytes: 39300