#include <bits/stdc++.h>
using namespace std;

void update(vector<vector<int64_t> > &kirai, int x, int y) {
    if (kirai.at(x).at(y-1) > kirai.at(x).at(y) + 1) {
        kirai.at(x).at(y-1) = kirai.at(x).at(y) + 1;
        update(kirai, x, y-1);
    } 
    if (kirai.at(x).at(y+1) > kirai.at(x).at(y) + 1) {
        kirai.at(x).at(y+1) = kirai.at(x).at(y) + 1;
        update(kirai, x, y+1);
    } 
    if (kirai.at(x-1).at(y) > kirai.at(x).at(y) + 1) {
        kirai.at(x-1).at(y) = kirai.at(x).at(y) + 1;
        update(kirai, x-1, y);
    } 
    if (kirai.at(x+1).at(y) > kirai.at(x).at(y) + 1) {
        kirai.at(x+1).at(y) = kirai.at(x).at(y) + 1;
        update(kirai, x+1, y);
    }
    return;
}

int main() {
    int n; cin >> n;
    vector<int> p(n*n);
    for (int i = 0; i < n*n; i++) {
        cin >> p.at(i);
    }
    vector<vector<int64_t> > kirai(n+2, vector<int>(n+2, -1));
    for (int k = 1; k <= (n+1)/2; k++) {
        for (int i = k; i <= n+1-k; i++) {            
            kirai.at(k).at(i) = k-1;
            kirai.at(n+1-k).at(i) = k-1;
            kirai.at(i).at(k) = k-1;
            kirai.at(i).at(n+1-k) = k-1;
        }
    }
    int64_t cnt = 0;
    for (int i = 0; i < n*n; i++) {
        int x = (p.at(i)-1)/n+1;
        int y = (p.at(i)-1)%n+1;
        cnt += kirai.at(x).at(y);
        kirai.at(x).at(y) = kirai.at(x-1).at(y);
        kirai.at(x).at(y) = min(kirai.at(x).at(y), kirai.at(x+1).at(y));
        kirai.at(x).at(y) = min(kirai.at(x).at(y), kirai.at(x).at(y-1));
        kirai.at(x).at(y) = min(kirai.at(x).at(y), kirai.at(x).at(y+1));
        update(kirai, x, y);
    }
    cout << cnt << endl;
}