#include <bits/stdc++.h>
using namespace std;


//SHORTCUTS

#define ll long long
#define endl '\n'
#define i1(a) cin>>a
#define i2(a,b) cin>>a>>b
#define i3(a,b,c) cin>>a>>b>>c
#define o1(a) cout<<a<<endl
#define o2(a,b) cout<<a<<" "<<b<<endl
#define o3(a,b,c) cout<<a<<" "<<b<<" "<<c<<endl
#define f(i,a,b) for(int i = a;i<b;i++)
#define fe(i,a,b) for(int i = a;i<=b;i++)
#define fi(i,a,b) for(i=a;i<b;i++)
#define eb emplace_back
#define pb push_back
#define pob pop_back
#define mp make_pair


// DEBUG CODE & FAST OUTPUT

#define printv(a) for (int i = 0; i < a.size(); i++) cout << a[i] << ' ';
#define printvl(a) for (int i = 0; i < a.size(); i++) cout << a[i] << endl;
#define printa(a, n) for (int i = 0; i< n; i++) cout << a[i] << ' ';
#define printal(a, n) for (int i = 0; i < n; i++) cout << a[i] << endl;


//DATA STRUCTURES

#define pq priority_queue
#define ms multiset
// #define y0 _y0_
// #define y1 _y1_
// #define div divi

typedef long double ld;
typedef vector <int> vi;
typedef vector <ll> vll;
typedef vector <ld> vld;
typedef vector <string> vs;
typedef pair <int, int> pii;
typedef pair <ll, ll> pll;
typedef vector <vi > vvi;
typedef vector <vll > vvll;
typedef vector <pii > vpii;
typedef vector <pll > vpll;


//IMP CONSTANT
const int mod = 1e9 + 7, inf = 1e9 + 7;
const ll infll = 1e18 + 7;
bool google_contest = false;

//MODULAR OPERATIONS
ll myadd(ll a,ll b){
    ll ans = a+b;
    ans-= ans>=mod ? mod : 0;
    return ans; 
}
ll mysub(ll a, ll b){
    return (a-b+mod)%mod;
}
ll mymul(ll a,ll b){
    return (a*b)%mod;
}
ll mypow(ll a, ll b){
    ll ans = 1;
    while(b>0)
        if(b&1) ans = mymul(ans,a),b--;
        else a = mymul(a,a),b >>= 1;
    return ans;
}
ll nCr(ll n,ll r){
    if(r==0)return 1;
    if(r==1)return n;
    if(n==0)return 0;
    return mymul(n,mymul(nCr(n-1,r-1),mypow(r,mod-2)));
}




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

//    /\    |  |\  /|                      
//   /__\   |  | \/ |           
//  /    \  |  |    | 

const int maxi = 505;
int dist[maxi][maxi];
vector<pair<int,int>> nei[maxi][maxi];
bool removed[maxi][maxi];

int main(){

ios_base::sync_with_stdio(0);
cin.tie(0); 
cout.tie(0);

int t,tt;
t=1;
// i1(t);
if(google_contest)tt=1;

while(t--){

int n;
i1(n);

f(i,0,n*n){
    int row = i/n;
    int col = i%n;
    f(j,0,4){
        int x = row + dx[j];
        int y = col + dy[j];
        if(x>=0 && x<n && y>=0 && y<n){
            nei[row][col].eb(x,y);
        }
    }
}


f(r,0,n){
    f(c,0,n){
        dist[r][c] = min({r,c,n-r-1,n-c-1});
    }
}


long long ans = 0;
f(i,0,n*n){
    int qq;
    i1(qq);
    qq--;
    int row = qq/n;
    int col = qq%n;
    ans+=dist[row][col];
    // cerr<<dist[row][col]<<endl;
    // dist[row][col]--;
    removed[row][col] = true;

    queue<pair<int,int>> q;
    q.push(make_pair(row,col));
    while(!q.empty()){
        auto top = q.front();
        q.pop();
        for(auto const &b : nei[top.first][top.second]){
            int curr = dist[top.first][top.second];
            if(!removed[top.first][top.second]){
                curr++;
            }
            if(dist[b.first][b.second]>curr){
                dist[b.first][b.second] = curr;
                q.push(b);
                // cerr<<b.first<<" "<<b.second<<endl;
            }
        }
    }

}
o1(ans);

if(google_contest){
    cout<<"Case "<<tt++<<": ";
}
}

return 0;   
}


Measured memory_bytes: 22400