#include<iostream>
#include<vector>
#include<string>
#include<map>
#include<set>
#include<queue>
#include<algorithm>
#include<cmath>
#include<iomanip>
#include<random>
#include<stdio.h>
using namespace std;

typedef long long ll;

void printVec(std::vector<int> &vec) {
  std::cout << "";
  for (auto it = vec.begin(); it != vec.end(); ++it) {
    std::cout << *it << " ";
  }
  std::cout << std::endl;
}

long long Mod(long long val, long long m) {
  long long res = val % m;
  if (res < 0) res += m;
  return res;
}

int INF=10000;


int main(){
    int N;
    cin>>N;
    vector<vector<int>> edge(N*N+1,vector<int>(0));
    for (int i=1;i<N*N+1;i++){
        if (i%N!=1){
            edge[i].push_back(i-1);
        }
        else{
            edge[i].push_back(0);
            edge[0].push_back(i);
        }
        if (i%N!=0){
            edge[i].push_back(i+1);
        }
        else{
            edge[i].push_back(0);
            edge[0].push_back(i);
        }
        if (i>N){
            edge[i].push_back(i-N);
        }
        else{
            edge[i].push_back(0);
            edge[0].push_back(i);
        }
        if (N*N-N+1>i){
            edge[i].push_back(i+N);
        }
        else{
            edge[i].push_back(0);
            edge[0].push_back(i);
        }
    }
    std::sort(edge[0].begin(),edge[0].end());
    edge[0].erase(std::unique(edge[0].begin(),edge[0].end()),edge[0].end());
    vector<int> P(N*N);
    for (int i=0;i<N*N;i++){
        cin>>P[i];
    }
    vector<int>  dist(N*N+1,INF);
    deque<pair<int,int>> que;
    pair<int,int> p;
    p.first=0;
    p.second=0;
    que.push_back(p);
    while (que.size()){
        pair<int,int> p=que.front();
        que.pop_front();
        int v=p.first;
        int d=p.second;
        if (dist[v]>d){
            dist[v]=d;
        }
        else{
            continue;
        }
        for (int i=0;i<edge[v].size();i++){
            int nv=edge[v][i];
            if (dist[nv]==INF){
                if (v==0){
                  	pair<int,int> p={nv,d};
                    que.push_front(p);
                }
                else{
                    pair<int,int> p={nv,d+1};
                    que.push_back(p);
                }
            }
        }
    }

    vector<bool> remove(N*N+1);
    remove[0]=1;
    ll ans=0;
    for (int i=0;i<N*N;i++){
        int V=P[i];
        ans+=dist[V];
        deque<pair<int,int>> que;
        pair<int,int> p={V,dist[V]};
        que.push_back(p);
        remove[V]=1;
        while (que.size()){
            pair<int,int> p=que.front();
            que.pop_front();
            int v=p.first;
            int d=p.second;
            if (dist[v]>d){
                dist[v]=d;
            }
            else if(v!=V){
                continue;
            }
            for (int j=0;j<edge[v].size();j++){
                int nv=edge[v][j];
                if (remove[v]){
                    int test=d;
                    pair<int,int> p={nv,test};
                    if (dist[nv]>test){
                        que.push_front(p);
                    }
                }
                else{
                    int test=d+1;
                    pair<int,int> p={nv,test};
                    if (dist[nv]>test){
                        que.push_back(p);
                    }
                }
            }
        }
    }
    cout<<ans<<endl;
}

Measured memory_bytes: 20940