#include <bits/stdc++.h>
#define F first
#define S second
#define mp make_pair
#define pb push_back
//#define ll __int128
#define ll long long
#define int long long
//#define int __int128
#define LEFT(a) ((a)<<1)
#define RIGHT(a) (LEFT(a) + 1)
#define MID(a,b) ((a+b)>>1)
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#define y1 y122

/*
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")

#pragma GCC target("avx2,fma")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
#pragma GCC target ("avx2")
#pragma GCC optimization ("unroll-loops")

#pragma comment(linker, "/STACK: 20000000005")
*/

using namespace std;


const int N = 505;

int n, ANS;

bool F[N][N];
int DP[N][N];

main()
{
    //ios_base::sync_with_stdio(0);
    cin >> n;
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++){
            F[i][j] = 1;
            DP[i][j] = min (min (i-1, n-i), min (j-1, n-j))+1;
        }
    int M = n*n;
    while (M--){
        int val;
        scanf ("%lld", &val);
        int x = (val-1) % n + 1;
        int y = (val-1) / n + 1;
        int ans = n+n;
        int pas = 0;
        for (int i = x; i <= n; i++){
            if (ans > pas + DP[i][y])
                ans = pas + DP[i][y];
            pas += F[i][y];
        }
        pas = 0;
        for (int i = x; i >= 1; i--){
            if (ans > pas + DP[i][y])
                ans = pas + DP[i][y];
            pas += F[i][y];
        }
        ans--;
        ANS += ans;
        DP[x][y] = ans;
        F[x][y] = 0;
        pas = ans;
        for (int j = y+1; j <= n; j++){
            pas += F[x][j];
            if (DP[x][j] > pas)
                DP[x][j] = pas;
        }
        pas = ans;
        for (int j = y-1; j >= 1; j--){
            pas += F[x][j];
            if (DP[x][j] > pas)
                DP[x][j] = pas;
        }
    }
    cout << ANS << endl;
}
