#include <algorithm>
#include <bitset>
#include <complex>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <locale>
#include <map>
#include <memory>
#include <new>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <typeinfo>
#include <utility>
#include <valarray>
#include <vector>
#include <climits>
#include <cstring>
#include <cassert>


using namespace std;
#define SZ(x) (int)(x.size())
#define REP(i, n) for(int i=0;i<n;++i)
#define FOR(i, a, b) for(int i=a;i<b;++i)
#define RREP(i, n) for(int i=n-1;i>=0;--i)
#define RFOR(i, a, b) for(int i=b-1;i>=a;--i)

#define all(x) begin(x),end(x)
#define dump(x) cerr<<#x<<" = "<<(x)<<endl
#define debug(x) cerr<<#x<<" = "<<(x)<<" (L"<<__LINE__<<")"<< endl;

using ll = long long;
using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using P = pair<int, int>;
const double eps = 1e-8;
const ll MOD = 1000000007;
const int INF = INT_MAX / 2;
const ll LINF = LLONG_MAX / 2;

template<class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return true;
    }
    return false;
}

template<class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return true;
    }
    return false;
}


template<typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> p) {
    os << p.first << ":" << p.second;
    return os;
}

template<class T>
ostream &operator<<(ostream &os, const vector<T> &v) {
    os << "[";
    REP(i, SZ(v)) {
        if (i) os << ", ";
        os << v[i];
    }
    return os << "]";
}

// edit

void solve() {
    int N;
    cin >> N;
    
    int NN = N * N;
    vector<int> P(NN);
    REP(i, NN) cin >> P[i];
    
    vector<pair<int,int>> Q(NN + 1, make_pair(-1, -1));
    
    for(int i = 0, n = 1; i < N; ++i){
        for(int j = 0; j < N; ++j, n++){
            Q[n] = make_pair(i, j);
        }
    }
    
//    cout << Q << endl;
    
    
    
    vector<vector<int>> dist(N, vector<int>(N, 0));
    vector<vector<bool>> block(N, vector<bool>(N, true));
    vector<int> ans;
    
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            int tapi = min(i, N - 1 - i);
            int tapu = min(j, N - 1 - j);
            dist[i][j] = min(tapi, tapu);
        }
    }
    
//    RREP(i, NN){
//    for(int i = NN; i > 0; --i){
    for(int i = 1; i <= NN; ++i){
        int r, c;
        tie(r, c) = Q[P[i - 1]];
        
        ans.push_back(dist[r][c]);
        
        
        //更新
        
        queue<pair<int,int>> que;
        que.push(make_pair(r, c));
//        dist[r][c]--;
        block[r][c] = false;
        
        while(!que.empty()){
            int r,c;
            tie(r,c) = que.front();
            que.pop();

            int dr[4] = {0,1,0,-1};
            int dc[4] = {-1,0,1,0};
            
            for(int k = 0; k < 4; ++k){
                int nr = r + dr[k];
                int nc = c + dc[k];
                if(!(nr >= 0 && nr < N && nc >= 0 && nc < N)){
                    continue;
                }
                
                if(dist[nr][nc] > dist[r][c] + block[r][c]){
                    dist[nr][nc] = dist[r][c] + block[r][c];
                    que.push(make_pair(nr, nc));
                }
                
//                int min_val = dist[nr][nc];
//                int min_val = INF;
//
//                for(int l = 0; l < 4; ++l){
//                    int nnr = nr + dr[l];
//                    int nnc = nc + dc[l];
//                    if(!(nnr >= 0 && nnr < N && nnc >= 0 && nnc < N)){
//                        min_val = 0;
//                        continue;
//                    }
//                    chmin(min_val, dist[nnr][nnc] + block[nnr][nnc]);
//                }
//
//                if(dist[nr][nc] < min_val){
//                    dist[nr][nc] = min_val;
//                    que.push(make_pair(nr, nc));
//                }
            }
            
//            cerr << "----" << endl;
//            for(int i = 0; i < N; ++i){
//                for(int j = 0; j < N; ++j){
//                    cerr << dist[i][j] << " ";
//                }
//                cerr << endl;
//            }
//            cerr << endl;
//            for(int i = 0; i < N; ++i){
//                for(int j = 0; j < N; ++j){
//                    cerr << block[i][j] << " ";
//                }
//                cerr << endl;
//            }
        }
    }
    
//    reverse(all(ans));
    
    ll sum = accumulate(all(ans), 0);
    
    cout << sum << endl;

    
    
    
}

void sample(int N){
    vector<int> P;
    for(int i = 0; i < N; ++i){
        for(int j = 0; j < N; ++j){
            P.push_back((int)P.size() + 1);
        }
    }
//    cout << P << endl;
    
    REP(i, N * N){
        int a, b;
        a = rand() % (N * N);
        b = rand() % (N * N);
        swap(P[a], P[b]);
    }
    
    cout << N << endl;
    
    for(int i = 0; i < N * N; ++i){
            cout << P[i] << " ";
    }
    cout << endl;
    
    
}


int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
//    std::ifstream in("input.txt");
//    std::cin.rdbuf(in.rdbuf());
//    sample(500);

    solve();


    return 0;
}


Measured memory_bytes: 7824