#include <bits/stdc++.h>
#define ALL(obj) begin(obj), end(obj)
#define debug(x) cerr << #x << ": " << x << '\n'
using namespace std;
template <class T>
vector<T> make_vec(size_t a) {
    return vector<T>(a);
}
template <class T, class... Ts>
auto make_vec(size_t a, Ts... ts) {
    return vector<decltype(make_vec<T>(ts...))>(a, make_vec<T>(ts...));
}
template <class T>
bool chmax(T &a, const T &b) {
    if (a < b) {
        a = b;
        return 1;
    }
    return 0;
}
template <class T>
bool chmin(T &a, const T &b) {
    if (b < a) {
        a = b;
        return 1;
    }
    return 0;
}

using ll = long long;
using ull = unsigned long long;
const int INF = 1e9;
// const int MOD = 1e9 + 7;
// 1次元 vector
template <class T>
ostream &operator<<(ostream &s, vector<T> vec) {
    for (int i = 0; i < (int)vec.size(); ++i) {
        if (i > 0) {
            s << " ";
        }
        s << vec[i];
    }
    return s << endl;
}
template <class T>  // 2次元 vector
ostream &operator<<(ostream &s, vector<vector<T>> vec2) {
    for (int i = 0; i < (int)vec2.size(); ++i) {
        s << vec2[i];
    }
    return s;
}

ll N;
pair<int, int> decode(int id) {
    return make_pair(id / N, id % N);
}
int dx[4] = {1, -1, 0, 0};
int dy[4] = {0, 0, 1, -1};  // dx,dy は次の探索場所への距離を表す（４方向分）

void dfs(int i, int j, vector<vector<ll>> &A, vector<vector<bool>> &B) {
    if (B[i][j]) A[i][j]--;
    for (int t = 0; t < 4; t++) {
        int x = i + dx[t];
        int y = j + dy[t];
        if (y < 0 || N <= y || x < 0 || N <= x) continue;
        if (A[x][y] > A[i][j]) {
            dfs(x, y, A, B);
        }
    }
    if (!B[i][j]) A[i][j]--;
};
int main() {
    cin >> N;
    vector<int> P(N * N);
    for (int i = 0; i < N * N; i++) {
        cin >> P.at(i);
        P[i]--;
    }
    /*
    for (int i = 0; i < N * N; i++) {
        auto [id, j] = decode(P[i]);
        cerr << id << " " << j << endl;
    }
    */
    auto A = make_vec<ll>(N, N);
    auto B = make_vec<bool>(N, N);

    for (int i = 1; i < N; i++) {
        int j = N - i;
        if (i < j) {
            A[i][i]++;
            A[i][j]--;
            A[j][i]--;
            A[j][j]++;
        } else {
            break;
        }
    }
    for (int i = 1; i < N; i++) {
        for (int j = 0; j < N; j++) {
            A[i][j] += A[i - 1][j];
        }
    }
    for (int i = 0; i < N; i++) {
        for (int j = 1; j < N; j++) {
            A[i][j] += A[i][j - 1];
        }
    }

    ll ans = 0;
    for (int id = 0; id < N * N; id++) {
        auto [i, j] = decode(P[id]);
        ans += A[i][j];
        ll min_v = 1e18;
        ll max_v = 0;
        for (int t = 0; t < 4; t++) {
            int x = i + dx[t];
            int y = j + dy[t];
            if (y < 0 || N <= y || x < 0 || N <= x) {
                chmin(min_v, (ll)-1);
                continue;
            };
            chmin(min_v, A[x][y]);
            chmax(max_v, A[x][y]);
        }
        if (max_v > min_v + 1) {
            for (int t = 0; t < 4; t++) {
                int x = i + dx[t];
                int y = j + dy[t];
                if (y < 0 || N <= y || x < 0 || N <= x) continue;
                if (A[x][y] == max_v) {
                    dfs(x, y, A, B);
                }
            }
        }
        B[i][j] = true;
    }
    cout << ans << endl;
}
