#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)));
}




vi dx= {1, -1, 0,  0};
vi dy= {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);
//         }
//     }
// }

for(int row = 0; row < n; ++row) {
    for(int col = 0; col < n; ++col) {
        for(int dir = 0; dir < 4; ++dir) {
            int r2 = row + dx[dir];
            int c2 = col + dy[dir];
            if(0 <= r2 && 0 <= c2 && r2 < n && c2 < n) {
                nei[row][col].emplace_back(r2, c2);
            }
        }
    }
}
f(r,0,n){
    f(c,0,n){
        dist[r][c] = 1+ min({r,c,n-r-1,n-c-1});
    }
}

// vector<vector<bool>> removed(n,vector<bool>(n,false));

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]-1;
    // 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;
    //         }
    //     }
    //}
    vector<pair<int,int>> q;
    q.emplace_back(row, col);
    for(int i = 0; i < (int) q.size(); ++i) {
        pair<int,int> a = q[i];
        for(const pair<int,int>& b : nei[a.first][a.second]) {
            int maybe = dist[a.first][a.second];
            if(!removed[b.first][b.second]) {
                maybe++;
            }
            if(dist[b.first][b.second] > maybe) {
                dist[b.first][b.second] = maybe;
                q.push_back(b);
            }
        }
    }


}
o1(ans);

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

return 0;   
}


Measured memory_bytes: 25904