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

const int MAXN = 500+5;
int cost[MAXN][MAXN];
bool occ[MAXN][MAXN];
int ans, N;
int dx[4] = {0, 1, 0, -1};
int dy[4] = {1, 0, -1, 0};

void update(int x, int y)
{
    int val = cost[x][y] + occ[x][y];
    for(int i=0;i<4;i++){
        int x1 = x+dx[i];
        int y1 = y+dy[i];
        if(x1<=0 || y1<=0 || x1>N || y1>N) continue;
        if(cost[x1][y1]>val){
            cost[x1][y1] = val;
            update(x1, y1);
        }
    }
}

int main()
{
    cin >> N;
    for(int i=1;i<=N;i++){
        for(int j=1;j<=N;j++){
            int temp1 = min(i, N-i+1);
            int temp2 = min(j, N-j+1);
            cost[i][j] = min(temp1, temp2) - 1;
            occ[i][j] = true;
        }
    }

    ans = 0;
    int cnt = N*N;
    while(cnt--)
    {
        int indx; cin >> indx;
        int x = indx/N+1;
        int y = indx%N;
        if(y==0) y+=N, x-=1;
        ans+=cost[x][y];
        occ[x][y] = false;
        update(x, y);
    }
    cout << ans << endl;
    return 0;
}

