import sys


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

    Kcost = [min(row, N - 1 - row, clm, N - 1 - clm) for row in range(N) for clm in range(N)]

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


    #def Kcheck(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()
            div, mod = divmod(qsn, N)
            if mod: #Kcheck(qsn, qsn - 1)
                cdsn = qsn - 1
                if Kcost[cdsn] > Kcost[qsn] + Sitting[qsn]:
                    Kcost[cdsn] = Kcost[qsn] + Sitting[qsn]
                    queue.append(cdsn)
            if mod != N - 1: #Kcheck(qsn, qsn + 1)
                cdsn = qsn + 1
                if Kcost[cdsn] > Kcost[qsn] + Sitting[qsn]:
                    Kcost[cdsn] = Kcost[qsn] + Sitting[qsn]
                    queue.append(cdsn)
            if div: #Kcheck(qsn, qsn - N)
                cdsn = qsn - N
                if Kcost[cdsn] > Kcost[qsn] + Sitting[qsn]:
                    Kcost[cdsn] = Kcost[qsn] + Sitting[qsn]
                    queue.append(cdsn)
            if div != N - 1: #Kcheck(qsn, qsn + N)
                cdsn = qsn + N
                if Kcost[cdsn] > Kcost[qsn] + Sitting[qsn]:
                    Kcost[cdsn] = Kcost[qsn] + Sitting[qsn]
                    queue.append(cdsn)

    print(Kcount)


if __name__ == '__main__':
    main()

sys.exit()
