#include <algorithm>
#include <assert.h>
#include <bitset>
#include <chrono>
#include <cstring>
#include <functional>
#include <iostream>
#include <istream>
#include <map>
#include <numeric>
#include <ostream>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>

#include <iomanip>
#include <math.h>
#include <cmath>
#include <array>

#include <experimental/optional>
#define optional std::experimental::optional

namespace asl
{
    
    template <typename T, typename U = T>
    optional<U> noop_get_lazy(T &p)
    {
        return optional<U>();
    }

    template <typename T>
    T add_merge(T &l, T &r) { return l + r; }

} 

namespace asl
{
    
    template <typename T>
    std::istream &operator>>(std::istream &is, std::vector<T> &vec)
    {
        for (auto &value : vec)
            is >> value;
        return is;
    }

    int dx[4] = {-1, 0, +1, 0};
    int dy[4] = {0, +1, 0, -1};

    class TBoard
    {
    public:
        int n, m;

        TBoard(int n = 0, int m = 0) : n(n), m(m) {}

        bool inside(int x, int y)
        {
            return 0 <= x && x < n && 0 <= y && y < m;
        }

    };

    template <typename T>
    std::vector<std::vector<T>> board(int n, int m, T def)
    {
        return std::vector<std::vector<T>>(n, std::vector<T>(m, def));
    }

    template <typename T>
    std::vector<std::vector<T>> board(int n = 0, int m = 0)
    {
        return std::vector<std::vector<T>>(n, std::vector<T>(m));
    }

    enum BFSResult
    {
        SKIP = 0,
        ADD = 1,
        FIN = 2,
    };

    void bfs4(int sx, int sy, std::function<BFSResult(int, int, int, int)> fn)
    {
        std::queue<std::pair<int, int>> q;
        
        if (fn(sx, sy, -1, -1) == BFSResult::ADD)
        {
            q.push({sx, sy});
        }

        bool fin = false;

        while (!fin && !q.empty())
        {
            int x, y;
            std::tie(x, y) = q.front();
            q.pop();

            for (int k = 0; k < 4; ++k)
            {
                int nx = x + dx[k], ny = y + dy[k];

                auto res = fn(nx, ny, x, y);

                if (res == BFSResult::ADD)
                {
                    q.push({nx, ny});
                }
                else if (res == BFSResult::FIN)
                {
                    fin = true;
                    break;
                }
            }
        }
    }

    void bfs4(int sx, int sy, std::function<bool(int, int, int, int)> fn)
    {
        std::function<BFSResult(int, int, int, int)> inner_fn = [&](int x, int y, int fx, int fy) {
            auto res = fn(x, y, fx, fy);
            return res ? BFSResult::ADD : BFSResult::SKIP;
        };

        bfs4(sx, sy, inner_fn);
    }
} 

#define endl '\n'

using namespace std;
using namespace asl;

int main()
{
    ios_base::sync_with_stdio(0);
    cin.tie(0);

    int n;
    cin >> n;

    vector<int> p(n * n);
    cin >> p;

    auto b = board<int>(n, n);
    auto u = board<int>(n, n, true);

    for (int i = 0; i < n; ++i)
    {
        for (int j = 0; j < n; ++j)
        {
            b[i][j] = 1 + min({i, j, n - i - 1, n - j - 1});
        }
    }

    TBoard t(n, n);

    long long answer = 0;

    for (auto i : p)
    {
        i--;
        int x = i / n;
        int y = i % n;

        int ttt = 10;

        answer += b[x][y] - 1;
        u[x][y] = false;

        bfs4(x, y, [&](int x, int y, int fx, int fy) {
            if (!t.inside(x, y))
                return false;

            int dist = 0;

            if (fx == -1)
            {
                dist = b[x][y] - 1;
            }
            else
            {
                dist = b[fx][fy] + u[x][y];
            }

            if (b[x][y] > dist)
            {
                b[x][y] = dist;
                return true;
            }
            else
            {
                return false;
            }
        });
    }

    cout << answer << endl;

    return 0;
}


