#pragma GCC optimize("Ofast")
  
#include <iostream>
#include <algorithm>
#include <vector>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <queue>
#include <stack>
#include <deque>
#include <set>
#include <unordered_set>
#include <list>
#include <tuple>
#include <iomanip>
#include <climits>
#include <cstdlib>
#include <cassert>
#include <numeric>
#include <math.h>
#include <bitset>
#include <random>
#include <regex>

using namespace std;

#define rep(i,n) for(int i=0;i<(n);++i)
#define reps(i,f,n) for(int i=f;i<=(n);++i)
#define all(x) (x).begin(),(x).end()
#define Fixed fixed << setprecision(4)
#define int long long
using pii = pair<int,int>;
using Graph = vector<vector<pii> >;
constexpr int INF  = 0x3f3f3f3f;
constexpr long long LINF = 0x3f3f3f3f3f3f3f3fLL;
constexpr int mod1 = 1e9+7; 
constexpr int mod2 = 998244353;
constexpr int dy[] = {0,1,0,-1,1,-1,-1,1};
constexpr int dx[] = {1,0,-1,0,1,1,-1,-1};
 
template <class A, class B> inline bool chmax(A &a, const B &b) { return b > a && (a = b, true); }
template <class A, class B> inline bool chmin(A &a, const B &b) { return b < a && (a = b, true); }

template <class A, class B> inline A updiv(const A a,const B b){ return (a + b - 1) / b; }
template <class A, class B> inline A gcd(const A a,const B b){ return b ? gcd(b,a % b) : a; }
template <class A, class B> inline A lcm(const A a,const B b){ return a / gcd(a,b) * b; }

template <class T> using min_heap = priority_queue<T,vector<T>,greater<T> >;
template <class T> using max_heap = priority_queue<T>;
template <class A,class B> using umap = unordered_map<A,B>;


signed main(void){
    cin.tie(nullptr);
    ios_base::sync_with_stdio(false);

    int n, cnt = 1;
    cin >> n;

    vector<int> p(n*n);
    vector<pii> pos(n*n+1);
    vector<vector<int> > seat(n, vector<int>(n, 1));
    vector<pii> vec;

    rep(i,n){
        rep(j,n){
            pos[cnt] = {i, j};
            ++cnt;
        }
    }

    for(auto&& e : p){
        cin >> e;
    }

    auto inside = [&n](int y, int x){
        return (0 <= y && y < n && 0 <= x && x < n);
    };

    int res = 0;

    for(auto&& e : p){
        int sy = pos[e].first;
        int sx = pos[e].second;
        seat[sy][sx] = 0;
        vector<vector<int> > dist(n, vector<int>(n, INF));
        queue<pii> que;
        dist[sy][sx] = 0;
        que.emplace(sy, sx);
        int minc = INF;
        while(!que.empty()){
            auto& [y, x] = que.front();
            que.pop();
            rep(i,4){
                int ny = y + dy[i];
                int nx = x + dx[i];
                if(inside(ny, nx)){
                    if(chmin(dist[ny][nx], dist[y][x] + seat[ny][nx])){
                        que.emplace(ny, nx);
                    }
                }else{
                    chmin(minc, dist[y][x]);
                }
            }
        }
        res += minc;
    }

    cout << res << '\n';

    return 0;
}