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

int t;
vector<vector<int>> seats;

void bfs(int x, int y) {
    queue<pair<int,int>> q;
    q.push({x,y});
    while(!q.empty()) {
        auto&& xy = q.front(); q.pop();
        int x = xy.first, y = xy.second;
        if(y > 0 && seats[y - 1][x] > seats[y][x] +  1) {
            seats[y - 1][x] = seats[y][x] +  1;
            q.push({x, y - 1});
        }
        if(y < t - 1&& seats[y + 1][x] > seats[y][x] +  1) {
            seats[y + 1][x] = seats[y][x] +  1;
            q.push({x, y + 1});
        }
        if(x > 0 && seats[y][x - 1] > seats[y][x] +  1) {
            seats[y][x - 1] = seats[y][x] +  1;
            q.push({x - 1, y);
        }
        if(x < t - 1 && seats[y][x + 1] > seats[y][x] +  1) {
            seats[y][x + 1] = seats[y][x] +  1;
            q.push({x + 1, y});
        }
    }
}

int main() {
    cin >> t;
    for(int y = 0; y < t; y++) {
        vector<int> row;
        for(int x = 0; x < t; x++) {
            row.push_back(min({x, t - x - 1, y, t - y - 1}));
        }
        seats.push_back(row);
    }
    int result = 0;
    for(int i = 0; i < t * t; i++) {
        int number;
        cin >> number;
        number--;
        int x = number % t;
        int y = number / t;
        result += seats[y][x];
        seats[y][x]--;
        bfs(x, y);
    }
    cout << fixed << result;
}
