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

#define watch(x) cerr << (#x) << ": " << (x) << endl

typedef long long LL;

const int N = 509;
int n, p[N*N];

int grid[N][N];
int here[N][N];

int dx[4] = {-1, 1, 0, 0}, dy[4] = {0, 0, -1, 1};

void dfs(int x, int y) {
    int cur = grid[x][y] + here[x][y];
    for(int i = 0; i < 4; ++i) {
        int xx = x + dx[i], yy = y + dy[i];
        if(grid[xx][yy] > cur) {
            grid[xx][yy] = cur;
            dfs(xx, yy);
        }
    }
}

int main() {

    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n;
    for(int i = 0; i < n*n; ++i) {
        cin >> p[i];
    }
    
    for(int i = 0; i < n; ++i) {
        for(int j = 0; j < n; ++j) {
            int l = i, r = n-1-i, u = j, d = n-1-j;
            grid[i][j] = min(l, min(r, min(u, d)));
            here[i][j] = 1;
        }
    }

    int ans = 0;

    for(int i = 0; i < n*n; ++i) {
        int cur = p[i]-1, x = cur/n, y = cur%n;
        ans += grid[x][y];
        
        here[x][y] = 0;
        dfs(x, y);
    }

    cout << ans << '\n';
    
    return 0;
}
