#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>

#define ll long long
#define lst(x) x.size()-1
#define ld long double
#define pr pair<ll,ll>
#define loop(i,n) for(int i=0 ; i<n ; ++i)
#define rep(i,x,n) for(int i=x ; i<=n ; ++i)
#define iteloop(type,data,name,it) for(type<data>::iterator it=name.begin() ;it!=name.end() ; ++it)

using namespace __gnu_pbds;
using namespace std;

template<typename T>
using os = tree<T, null_type, less<T>, rb_tree_tag,
        tree_order_statistics_node_update>;
const int D=5,D2=5e2+5;
const ll N=D2,M=1e1+5,MOD=1e9+7,OO=1e9;
int arr[N*N],n;
vector<int> conn(int x){
    vector<int> res;
    if(x%n != 0)
        res.push_back(x+1);
    if((x-1)%n != 0)
        res.push_back(x-1);
    if(x+n<=n*n)
        res.push_back(x+n);
    if(x-n >= 1)
        res.push_back(x-n);
    return res;
}
pair<int,int> getInd(int x){
    int row=(x-1)/n;
    int col=x-row*n-1;
    return {row,col};
}
int ans[N][N];
bool taken[N][N];
int main(){
    cin.sync_with_stdio(false), cin.tie(0) , cout.tie(0);
#ifndef ONLINE_JUDGE
    freopen("std/in.txt", "rt" , stdin);
    // freopen("in.txt", "wt" , stdout);
#endif // ONLINE_JUDGE
    scanf("%d",&n);
    loop(i,n*n){
        scanf("%d",&arr[i]);
    }
    loop(i,n){
        loop(j,n){
            ans[i][j] = min({i,j,n-i-1,n-j-1});
        }
    }
    int res=0;
    loop(i,n*n){
        int x=arr[i];
        if(x==9){
            x=9;
        }
        auto p=getInd(x);
        taken[p.first][p.second]=true;
        res+=ans[p.first][p.second];
        queue<pair<int,int>> q;
        auto nbrs=conn(x);
        for(int g:nbrs){
            int cost = ans[p.first][p.second];
            auto pg=getInd(g);
            if(ans[pg.first][pg.second]>cost){
                ans[pg.first][pg.second]=cost;
                q.push({g,cost});
            }
        }
        while(!q.empty()){
            p=q.front();
            q.pop();
            auto p2=getInd(p.first);
            nbrs=conn(p.first);
            for(int g:nbrs){
                int cost = p.second+!taken[p2.first][p2.second];
                auto pg=getInd(g);
                if(ans[pg.first][pg.second]>cost){
                    ans[pg.first][pg.second]=cost;
                    q.push({g,cost});
                }
            }
        }
    }
    printf("%d\n",res);
}