#include <bits/stdc++.h>
using namespace std;
template <class T> inline bool chmax(T &a, T b) {
    if(a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T> inline bool chmin(T &a, T b) {
    if(a > b) {
        a = b;
        return 1;
    }
    return 0;
}

#ifdef DEBUG
void debug() { cerr << "\n"; }
template <class T> void debug(const T &x) { cerr << x << "\n"; }
template <class T, class... Args> void debug(const T &x, const Args &... args) {
    cerr << x << " ";
    debug(args...);
}
template <class T> void debugVector(const vector<T> &v) {
    for(const T &x : v) {
        cerr << x << " ";
    }
    cerr << "\n";
}
#else
template <class T, class... Args>
void debug(const T &x, const Args &... args) {}
template <class T> void debugVector(const vector<T> &v) {}
#endif

using ll = long long;
#define ALL(v) (v).begin(), (v).end()
#define RALL(v) (v).rbegin(), (v).rend()
const double EPS = 1e-7;
const int INF = 1 << 30;
const ll LLINF = 1LL << 60;
constexpr int MOD = 1000000007;
const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};

//-------------------------------------

int N;
vector<int> p;
vector<vector<bool>> exist;

int bfs(int s) {
    vector d(N * N, INF);
    queue<int> que;
    que.emplace(s);
    d[s] = 0;
    while(!que.empty()) {
        int now = que.front();
        que.pop();
        int i = now / N;
        int j = now % N;
        for(int k = 0; k < 4; k++) {
            int ni = i + dy[k];
            int nj = j + dx[k];
            if(ni < 0 || ni >= N || nj < 0 || nj >= N) {
                continue;
            }
            if(d[ni * N + nj] > d[now] + exist[ni][nj]) {
                d[ni * N + nj] = d[now] + exist[ni][nj];
                que.emplace(ni * N + nj);
            }
        }
    }
    int res = INF;
    for(int i = 0; i < N; i++) {
        chmin(res, min(d[i * N], d[i * N + N - 1]));
    }
    for(int j = 0; j < N; j++) {
        chmin(res, min(d[j], d[(N - 1) * N + j]));
    }
    return res;
}

int main() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cin >> N;
    p.resize(N * N);
    for(int i = 0; i < N * N; i++) {
        cin >> p[i];
        p[i]--;
    }
    exist.resize(N, vector<bool>(N, true));
    int ans = 0;
    for(auto s : p) {
        ans += bfs(s);
        exist[s / N][s % N] = false;
    }
    cout << ans << "\n";
}