#include <bits/stdc++.h>
using namespace std;
__int128 cost;
#define ld long double
#define li __int128
#define ll long long
#define int long long
main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout << fixed << setprecision(10);
    int n;
    cin >> n;
    vector <int> P(n*n);
    for(int i = 0; i < n*n; ++i)cin >> P[i];
    vector <vector <int> > a(n, vector <int> (n, 1)); 
    int odp = 0;
    for(int h = 0; h < n*n; ++h){
        int i = P[h] - 1; 
        int wi = i/n;
        int kol = i%n;
        a[wi][kol] = 0;
        int a1,a2,a3,a4; 
        a1=a2=a3=a4 = 0; 
        for(int j = wi-1; j >= 0; --j){
            if(a[j][kol])a1++;
        }
        for(int j = wi+1; j < n; ++j){
            if(a[j][kol])a2++;
        }
        
        for(int j = kol+1; j < n; ++j){
            if(a[wi][j])a3++;
        }
        for(int j = kol-1; j>= 0; --j){
            if(a[wi][j])a4++;
        }
        odp += min(min(a1,a2), min(a3,a4));
    }
    cout << odp << '\n';
}
