#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("sse4")

using namespace std;

#define FOR(a,b) for(int a=0;a<b;a++)
#define F0R(a,b,c) for(int a = b; a<=c; a++)
#define f first
#define s second
#define m0(x) memset(x,0,sizeof(x))

typedef pair<int,int> pi;
typedef long long ll;
typedef vector<int> vi;
typedef vector<pi> vpi;

int cost[502][502]; int n;
bool z[502][502];
int xmove[4] = {0,0,-1,1};
int ymove[4] = {1,-1,0,0};

void dfs(int x, int y){
    assert(x > 0);
    assert(y > 0);
    FOR(i,4){
        if(cost[x+xmove[i]][y+ymove[i]] > cost[x][y]+((int)z[x][y])){
            cost[x+xmove[i]][y+ymove[i]] = cost[x][y]+((int)z[x][y]);
            dfs(x+xmove[i],y+ymove[i]);
        }
    }
}

int main()
{
    ios_base::sync_with_stdio(0); cin.tie(0);
    cin >> n; m0(cost);
    F0R(i,1,n) F0R(j,1,n) z[i][j] = 1;
    F0R(i,1,n) F0R(j,1,n) cost[i][j] = min(min(i-1,n-i),min(j-1,n-j));
    int ans = 0;
    FOR(i,n*n){
        int a; cin >> a;
        int x = ((a-1)/n) + 1, y = (a-1)%n + 1;
        ans += cost[x][y];
        z[x][y] = 0;
        dfs(x,y);
    }
    cout << ans << endl;
    return 0;
}
