#include <bits/stdc++.h>
#ifdef __DEBUG__
    #define DBG(X) cout << #X << " = " << (X) << endl;
    #define SAY(X) cout << (X) << endl;
#else
    #define DBG(X)
    #define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
using pll = pair<ll, ll>;

int dx[]={1, 0, -1, 0, 1,-1, 1,-1};
int dy[]={0, 1, 0, -1, 1, 1,-1,-1};
const int INT_INF = (int)(2e9);
const ll  LL_INF = (ll)(2e18);

inline void fast_io() { ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr); };
template<typename T, typename S> inline ostream& operator<<(ostream& os, const pair<T, S> p) { cout << "[" << p.first << ";" << p.second << "]"; return os; }
template<typename T, typename S> inline ostream& operator<<(ostream& os, const map<T, S> p) { for (auto el : p) cout << "[" << el.first << ";" << el.second << "]"; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const vector<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const deque<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline ostream& operator<<(ostream& os, const set<T>& v) { for (auto el : v) cout << el << " "; return os; }
template<typename T> inline vector<T> fetch_vec(int sz) { vector<T> ret(sz); for (auto& elem : ret) cin >> elem; return ret; }

int N;
vector<int> P;
// vector<string> S;
void input(){
    fast_io();
    cin >> N;
    for (int i = 0; i < N*N; i++)
    {
        int p; cin >> p;
        p--;
        P.push_back(p);
    }
}

int dist(int i, int j){
    int ret=N;
    ret = min(ret,i);
    ret = min(ret,N-1-i);
    ret = min(ret,j);
    ret = min(ret,N-1-j);
    return ret;
}

int dist_2(int nx, int ny, int x, int y){
    return abs(nx-x)+abs(ny-y);
}

vector<vector<bool>> seen;
vector<vector<int>> bfs(int const &x, int const &y, vector<vector<int>> D){
    queue<piii> q;
    q.emplace(0, make_pair(x,y));
    while(!q.empty()){
        int d = q.front().first;
        int cx = q.front().second.first;
        int cy = q.front().second.second;
        q.pop();
        for (int k = 0; k < 4; k++)
        {
            int nx = cx+dx[k];
            int ny = cy+dy[k];
            if(nx==x && ny==y) continue;
            if(nx<0 || nx>=N) continue;
            if(ny<0 || ny>=N) continue;
            // int nd = dist_2(nx,ny,x,y)-1;
            int nd;
            if(seen[nx][ny]) nd = d;
            else nd = d+1;
            if(D[nx][ny]>(D[cx][cy]+nd-1)){
                D[nx][ny] = max(D[cx][cy]+nd-1,0);
                q.emplace(nd,make_pair(nx,ny));
            }
        }
    }
    return D;
}

vector<vector<int>> D;
void dfs(int x, int y){
    int d = D[x][y] + !seen[x][y];
    for (int k = 0; k < 4; k++)
    {
        int nx = x+dx[k];
        int ny = y+dy[k];
        if(nx<0 || nx>=N) continue;
        if(ny<0 || ny>=N) continue;

        if(D[nx][ny]>d){
            D[nx][ny]=d;
            dfs(nx,ny);
        }
    }
}

int solve(){
    D.assign(N,vector<int>(N,0));
    seen.assign(N,vector<bool>(N,false));
    for(int i = 0; i < N; i++) for(int j = 0; j < N; j++) D[i][j] = dist(i,j);
    ll ans = 0;
    for (int i = 0; i < N*N; i++)
    {
        int x = P[i]/N;
        int y = P[i]%N;
        ans += D[x][y];
        seen[x][y] = true;
        dfs(x,y);
        // for(auto d:D) cout << d << endl;
    }
    cout << ans << endl;
    return 0;
}   

int main()
{
    input();
    solve();
    return 0;
}

Measured memory_bytes: 20448