
import queue
import sys
import numpy as np
from numba import njit
input = sys.stdin.buffer.readline


@njit('(i4,i4[:])', cache=True)
def main(N, aa):
    mm = [[0] * N for _ in range(N)]

    for i in range((N + 1) // 2):
        j = N - i - 1

        for x in range(i, j + 1):
            mm[i][x] = i
            mm[j][x] = i
            mm[x][i] = i
            mm[x][j] = i

    is_none = [[False] * N for _ in range(N)]

    ans = 0

    for a in aa:
        x = (a - 1) % N
        y = (a - 1) // N

        ans += mm[y][x]

        is_none[y][x] = True

        mm[y][x] -= 1

        q = []
        q.append((x, y, mm[y][x]))
        idx = 0
        L = 1
        while idx < L:
            x0, y0, n = q[idx]

            if x0 != 0:
                if is_none[y0][x0 - 1] and mm[y0][x0 - 1] > n:
                    mm[y0][x0 - 1] = n
                    q.append((x0 - 1, y0, n))
                    L += 1
                elif not is_none[y0][x0 - 1] and mm[y0][x0 - 1] > n + 1:
                    mm[y0][x0 - 1] = n + 1
                    q.append((x0 - 1, y0, n + 1))
                    L += 1

            if x0 != N - 1:
                if is_none[y0][x0 + 1] and mm[y0][x0 + 1] > n:
                    mm[y0][x0 + 1] = n
                    q.append((x0 + 1, y, n))
                    L += 1
                elif not is_none[y0][x0 + 1] and mm[y0][x0 + 1] > n + 1:
                    mm[y0][x0 + 1] = n + 1
                    q.append((x0 + 1, y, n + 1))
                    L += 1

            if y0 != 0:
                if is_none[y0 - 1][x0] and mm[y0 - 1][x0] > n:
                    mm[y0 - 1][x0] = n
                    q.append((x0, y0 - 1, n))
                    L += 1
                elif not is_none[y0 - 1][x0] and mm[y0 - 1][x0] > n + 1:
                    mm[y0 - 1][x0] = n + 1
                    q.append((x0, y0 - 1, n + 1))
                    L += 1

            if y0 != N - 1:
                if is_none[y0 + 1][x0] and mm[y0 + 1][x0] > n:
                    mm[y0 + 1][x0] = n
                    q.append((x0, y0 + 1, n))
                    L += 1
                elif not is_none[y0 + 1][x0] and mm[y0 + 1][x0] > n + 1:
                    mm[y0 + 1][x0] = n + 1
                    q.append((x0, y0 + 1, n + 1))
                    L += 1

            idx += 1

    return ans


if __name__ == "__main__":
    N = int(input())
    aa = np.array(list(map(int, input().split())), np.int32)
    print(main(N, aa))
