### BUGGY CODE ###

from collections import deque


def to_pos(y, x):
    return y * n + x


def to_coord(pos):
    return pos // n, pos % n


def update(pos):
    que = deque()
    que.append((pos, cost[pos] - 1))
    while que:
        pos, dist = que.popleft()
        if cost[pos] <= dist:
            continue

        y, x = to_coord(pos)
        cost[pos] = dist

        if y >= 1:
            pp = to_pos(y - 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if y <= n - 2:
            pp = to_pos(y + 1, x)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x >= 1:
            pp = to_pos(y, x - 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))
        if x <= n - 2:
            pp = to_pos(y, x + 1)
            cc = int(there[pp])
            que.append((pp, dist + cc))


n = int(input())
p = map(lambda x: int(x) - 1, input().split())

there = [True] * (n * n)
cost = [min(i, n - i - 1, j, n - j - 1) for j in range(n) for i in range(n)]

ans = 0
for pos in p:
    ans += cost[pos]
    there[pos] = False
    update(pos)

print(ans)


### FIXED CODE ###

from numba import njit
import numpy as np


def cc_export():
    @njit('(i8, i8, i8[:], i8[:], i8[:], i8)')
    def update(n, w, que, there, cost, pos):
        que[0] = pos
        i, l = 0, 1
        while i < l:
            pos = que[i]
            i += 1
            c = cost[pos]
            t = there[pos]
            k = c + t
            for df in (-w, w, -1, 1):
                pp = pos + df
                if cost[pp] > k:
                    cost[pp] = k
                    que[l] = pp
                    l += 1

    @njit('(i8, i8[:])')
    def main(n, p):
        w = n + 2
        que = np.zeros(4 * w * w, dtype=np.int64)
        there = np.ones(w * w, dtype=np.int64)
        # there = np.array([
        #     1 if 1 <= i and i <= n and 1 <= j and j <= n else 0
        #     for j in range(w) for i in range(w)
        # ],
        #                  dtype=np.int64)
        cost = np.array([
            -1 if i == 0 or i == n + 1 or j == 0 or j == n + 1 else min(
                i - 1, n - i, j - 1, n - j) for j in range(w) for i in range(w)
        ],
                        dtype=np.int64)

        ans = 0
        for pos in p:
            # v = list(map(lambda x: ' ' if x == 0 else '*', list(there)))
            # for i in range(w):
            #     print(' '.join(v[i * w:(i + 1) * w]))
            # print(cost.reshape((w, w)))
            # print("add", cost[pos], "for", divmod(pos, w))
            ans += cost[pos]
            there[pos] = 0
            update(n, w, que, there, cost, pos)
        return ans

    from numba.pycc import CC
    cc = CC('solution')
    cc.export('update', '(i8, i8, i8[:], i8[:], i8[:], i8)')(update)
    cc.export('main', '(i8, i8[:])')(main)
    cc.compile()


if __name__ == '__main__':
    import sys
    if sys.argv[-1] == 'ONLINE_JUDGE':
        cc_export()
        sys.exit(0)

    def transform(t):
        y, x = divmod(int(t) - 1, n)
        return (y + 1) * (n + 2) + (x + 1)

    from solution import update, main
    n = int(input())
    p = np.array(list(map(transform, input().split())), dtype=np.int64)
    print(main(n, p))