//“Failure is unimportant. It takes courage to make a fool of yourself.”
#include "bits/stdc++.h"
#include "ext/pb_ds/assoc_container.hpp"
#include "ext/pb_ds/tree_policy.hpp"
typedef long long int ll;
#define int long long int
#define P 1000000007
#define sz(a) (ll)a.size()
#define lt(a) (ll)a.length()
#define pll pair<ll,ll>
#define f(i,a,b) for(ll i=(ll)a;i<(ll)b;i++)
#define bac(i,a,b) for(ll i=(ll)a;i>=(ll)b;i--)
#define mll map<ll,ll>
#define vl vector<ll>
#define vvl vector<vector<ll>>
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(),a.end()
#define F first
#define S second
#define teevra_gati ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL);
#define N 10000001
#define PI 3.14159265359
#define inf INT_MAX
using namespace __gnu_pbds;
using namespace std;

signed main() {
    teevra_gati
    
    ll n, ans=0,x;
    cin>>n;
    ll u[n+1][n+1]={};
    ll d[n+1][n+1]={};
    ll l[n+1][n+1]={};
    ll r[n+1][n+1]={};
    f(i,1,n+1) f(j,1,n+1) u[i][j]=i-1;
    f(i,1,n+1) f(j,1,n+1) d[i][j]=n-i;
    f(i,1,n+1) f(j,1,n+1) l[i][j]=j-1;
    f(i,1,n+1) f(j,1,n+1) r[i][j]=n-j;

    vector<pll> permut;
    
    f(i,0,n*n){
        cin>>x;
        //cout<<x<<endl;
        ll row = (x/n) + 1, col = (x%n);
        if(col==0) row--, col=n;
        ll mn = min({u[row][col], d[row][col], l[row][col], r[row][col]});
        ans += mn;
        //cout<<row<<" "<<col<<endl;
        //cout<<u[row][col]<<" "<<d[row][col]<<" "<<l[row][col]<<" "<<r[row][col]<<endl;
        
        f(k,row+1,n+1) if(u[k][col]) u[k][col]--;
        f(k,1,row) if(d[k][col]) d[k][col]--;
        f(k,1,col) if(r[row][k]) r[row][k]--;
        f(k,col+1,n+1) if(l[row][k]) l[row][k]--;
        /*f(i,1,n+1){
            f(j,1,n+1) cout<<d[i][j]<<" ";
            cout<<endl;
        }
        cout<<endl;*/
    }
    cout<<ans;
    return 0;
}