#include <bits/stdc++.h>
using namespace std;
#define endl '\n'
#define pi pair<int,int>
#define ff first
#define ss second
#define boost ios::sync_with_stdio(false);cin.tie(nullptr)
#include "string"
#define int long long 
int n;
int dp[501][501];
int a[501][501];

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

void dfs(int i,int j) {
    int cost = dp[i][j] + a[i][j];
    for(int l=0;l<4;l++){
        int x = i + dx[l];
        int y = j + dy[l];
        if(x<0 or y<0 or x>=n or y>=n)
            continue;
        if(dp[x][y] > cost){
            dp[x][y] = cost;
            dfs(x,y);
        }
    }
}

int32_t main() { 
    boost;
    cin >> n;
    vector<int>v(n*n);
    for(int &i : v)
        cin >> i;
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            dp[i][j] = min(min(i,n-i-1),min(j,n-j-1)) ;
            a[i][j] = 1;
        }
    }
    int ans = 0;
    for(int &i : v){
        i--;
        int x = i/n;
        int y = i%n;
        a[x][y] = 0;
        ans += dp[x][y];
        dfs(x,y);
    }
    cout<<ans<<endl;
}  
