#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define inf = 101010;
#define REP(i,j,n) for(int i=j;i<n;i++)
#define rep(i,n) REP(i,0,n)

const int MAXN = 503;
int H[MAXN][MAXN];
bool occ[MAXN][MAXN];
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};

void dfs(int x, int y)
{
    int z = H[x][y] + occ[x][y];
    rep(i,4)
    {
        int x1 = x+dx[i], y1 = y+dy[i];
        if (z < H[x1][y1])
        {
            H[x1][y1] = z;
            dfs(x1, y1);
        }
    }

}

// Generated by 1.1.7.1 https://github.com/kyuridenamida/atcoder-tools  (tips: You use the default template now. You can remove this line by using your custom template)
int main(){
    // Failed to predict input format
    int n;
    cin >> n;
    REP(i,1,n+1) REP(j,1,n)
    {
        occ[i][j] = true;
        H[i][j] = min(min(i-1, n-i), min(j-1, n-j)) ;
    }

    int res = 0;
    rep(i,n * n)
    {
        int idx;
        cin >> idx;
        idx--;
        int x = idx%n;
        int y = idx/n;
        x++;
        y++;
        res += H[x][y];
        occ[x][y] = false;
        dfs(x,y);
    }
    cout << res << endl;
    return 0;
}
