#define _CRT_SECURE_NO_WARNINGS
#include<vector>
#include<iostream>
#include<queue>
#include<algorithm>
#include<map>
#include<set>
#include<iomanip>
#include<assert.h>
#include<unordered_map>
#include<unordered_set>
#include<string>
#include<stack>
#include<complex>
#include<memory>
#include<numeric>
/*
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")*/
    
using namespace std;
    
using ll=long long;
using ld =long double;
    
#define WHATS(var)//cout<<__LINE__<<' '<<#var<<"="<<var<<endl;
    
template<class S, class T> ostream& operator <<(ostream &os, const pair<S, T> v){
    os << "( " << v.first << ", " << v.second << ")"; return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<T> &v){
    for(int i = 0; i < v.size(); i++){if(i > 0){os << " ";} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<vector<T>> &v){
    for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const vector<set<T>> &v){
    for(int i = 0; i < v.size(); i++){if(i > 0){os << endl;} os << v[i];} return os;
}
template<class T> ostream& operator <<(ostream &os, const set<T> &v){
    int i=0;
    for(auto it:v){
        if(i > 0){os << ' ';}
        os << it;
        i++;
    } 
    return os;
}
using Graph=vector<vector<int>>;
 
 
const int mod = 998244353;



bool operator<(const vector<int>&lv,const vector<int>&rv){
    
    if(lv.size()!=rv.size()){
        return lv.size()<rv.size();
    }else{
        for(int i=0;i<lv.size();++i){
            if(lv[i]!=rv[i]){
                return lv[i]<rv[i];
            }
        }
        return false;
    }
}

ll gcd(ll a,ll b){
    if(a%b==0)return b;
    else{
        return gcd(b,a%b);
    }
}

int gettp(int x,int y,int cx,int cy){
    return int (x>=cx) + int(y>=cy)*2;
}

string to(vector<int>v){
    string st;
    for(auto x:v){
        char ch;
        if(x<26)ch='a'+x;
        else if(x<52)ch='A'+(x-26);
        else ch='0'+(x-52);
        st.push_back(ch);
    }
    return st;
}

const int SIX_TWO=62;

vector<int>fin_answer;
int len;
int all_a_cnt;

int main() {
    ios::sync_with_stdio(false);
    cin.tie();
    int N;cin>>N;
    vector<vector<int>>field(N,vector<int>(N));
    vector<vector<int>>costs(N,vector<int>(N,10000));

    vector<int>v(N*N);
    for(int i=0;i<N*N;++i)cin>>v[i];
    for(int i=0;i<N*N;++i){
        v[i]--;
        int ay=v[i]/N;
        int ax=v[i]%N;
        field[ay][ax]=i;
    }
    vector<int>sums(N*N);
    for(int FREE_MAX=-1;FREE_MAX<N*N;++FREE_MAX){
        deque<pair<int,int>>que;
    
        if(FREE_MAX==-1){
            for(int y=0;y<N;++y){
                for(int x=0;x<N;++x){
                    if(y==0||y==N-1||x==0||x==N-1){
                        costs[y][x]=0;
                        que.push_back(make_pair(x,y));
                    }
                }
            }
        }else{
            int x=v[FREE_MAX]%N;
            int y=v[FREE_MAX]/N;
            sums[FREE_MAX]=costs[y][x];
            que.push_back(make_pair(x,y));
        }
        int dx[]={-1,0,1,0};
        int dy[]={0,1,0,-1};
        while(!que.empty()){

            auto q=que.front();
            que.pop_front();
            WHATS(q)
            int nx=q.first;
            int ny=q.second;
            int now_cost=costs[ny][nx];
            bool free_flag=field[ny][nx]<=FREE_MAX;
            int nex_cost=free_flag?now_cost:now_cost+1;
            for(auto way=0;way<4;++way){
                int ne_x=nx+dx[way];
                int ne_y=ny+dy[way];
                if(ne_x>=0&&ne_x<N&&ne_y>=0&&ne_y<N){
                    if(costs[ne_y][ne_x]>nex_cost){
                        costs[ne_y][ne_x]=nex_cost;
                        if(free_flag){
                            que.push_front(make_pair(ne_x,ne_y));
                        }else{
                            que.push_back(make_pair(ne_x,ne_y));
                        }
                    }
                }
            }
        }
        WHATS(costs)
    }
    int answer=accumulate(sums.begin(),sums.end(),0);
    cout<<answer<<endl;
    return 0;
}

Measured memory_bytes: 8176