import sys
from functools import lru_cache


def main():
    N = int(input())
    P = tuple(map(int, input().split()))

    Kcost = [0] * (N * N)
    for sn in range(N * N):
        row, clm = divmod(sn, N)
        Kcost[sn] = min(row, N - row - 1, clm, N - clm - 1)

    Sitting = [1] * (N * N)
    Kcount = 0


    #@lru_cache(maxsize=None)
    def Kcheck(csn, qsn, cdsn):
        if Kcost[cdsn] > Kcost[qsn] + Sitting[qsn]:
            Kcost[cdsn] = Kcost[qsn] + Sitting[qsn]
            queue.append(cdsn)


    for sn in P:
        csn = sn - 1
        Kcount += Kcost[csn]
        Sitting[csn] = 0
        queue = [csn]

        while queue:
            qsn = queue.pop()
            for dsn in (1, -1, N, -N):
                div, mod = divmod(qsn, N)
                if mod == 0 and dsn == -1: continue
                if mod == N - 1 and dsn == 1: continue
                if div == 0 and dsn == -N: continue
                if div == N - 1 and dsn == N: continue

                Kcheck(csn, qsn, qsn + dsn)

    print(Kcount)


if __name__ == '__main__':
    main()

sys.exit()
