import sys


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)

    Vseat = [0] * (N * N)
    Kcount = 0

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

        while queue:
            qsn = queue.pop()
            for dsn in (1, -1, N, -N):
                cdsn = qsn + dsn

                if qsn % N == 0 and dsn == -1 or qsn % N == N - 1 and dsn == 1  or cdsn < 0 or cdsn >= N:
                    continue

                if Vseat[cdsn] and Kcost[cdsn] > Kcost[qsn]:
                    Kcost[cdsn] = Kcost[qsn]
                    queue.append(cdsn)

                if Vseat[cdsn] == 0 and Kcost[cdsn] > Kcost[qsn] + 1:
                    Kcost[cdsn] = Kcost[qsn] + 1
                    queue.append(cdsn)

    print(Kcount)


if __name__ == '__main__':
    main()

sys.exit()
