import sys
#import numpy

sys.setrecursionlimit(10**6)
readline = sys.stdin.readline

def dfsr(i, j, cnt):
    global seet, N
    if j+1 < N:
        if seet[i][j+1] == -1:
            dfsr(i, j+1, cnt)
        else:
            if seet[i][j+1] > cnt:
                seet[i][j+1] = cnt
                dfs(i, j+1, cnt+1)

def dfsl(i, j, cnt):
    global seet, N
    if j-1 >= 0:
        if seet[i][j-1] == -1:
            dfsr(i, j-1, cnt)
        else:
            if seet[i][j-1] > cnt:
                seet[i][j-1] = cnt
                dfs(i, j-1, cnt+1)

def dfsu(i, j, cnt):
    global seet, N
    if i-1 >= 0:
        if seet[i-1][j] == -1:
            dfsr(i-1, j, cnt)
        else:
            if seet[i-1][j] > cnt:
                seet[i-1][j] = cnt
                dfs(i-1, j, cnt+1)

def dfsd(i, j, cnt):
    global seet, N
    if i+1 < N:
        if seet[i+1][j] == -1:
            dfsr(i+1, j, cnt)
        else:
            if seet[i+1][j] > cnt:
                seet[i+1][j] = cnt
                dfs(i+1, j, cnt+1)

def dfs(i, j, cnt):
    global seet, N
    if i+1 < N:
        if seet[i+1][j] > cnt+1:
            seet[i+1][j] = cnt+1
            dfs(i+1, j, cnt+1)
    if i-1 >= 0:
        if seet[i-1][j] > cnt+1:
            seet[i-1][j] = cnt+1
            dfs(i-1, j, cnt+1)
    if j+1 < N:
        if seet[i][j+1] > cnt+1:
            seet[i][j+1] = cnt+1
            dfs(i, j+1, cnt+1)
    if j-1 >= 0:
        if seet[i][j-1] > cnt+1:
            seet[i][j-1] = cnt+1
            dfs(i, j-1, cnt+1)

def main():
    global seet, N
    N = int(readline())
    P = list(map(int, readline().split()))

    seet = []
    n = N//2
    for i in range(N):
        if i < n:
            tmp = list(range(i+1)) + [i]*(N-2*(i+1)) + list(range(i,-1,-1))
            seet.append(tmp)
        if N%2 == 1 and i == n:
            tmp = list(range(i)) + [i] + list(range(i-1, -1, -1))
            seet.append(tmp)
        if N%2 == 1 and i > n:
            tmp = list(range(N-i)) + [N-i-1]*(N-2*(N-i)) + list(range(N-i-1, -1, -1))
            seet.append(tmp)
        if N%2 == 0 and i >= n:
            tmp = list(range(N-i)) + [N-i-1]*(N-2*(N-i)) + list(range(N-i-1, -1, -1))
            seet.append(tmp)

    #nseet = numpy.array(seet)

    ans = 0
    for p in P:
        i, j = (p-1)//N, (p-1)%N
        seetij = seet[i][j]
        ans += seetij
        if i+1 < N:
            if seet[i+1][j] == -1:
                dfsd(i+1, j, seetij)
            if seet[i+1][j] > seetij:
                seet[i+1][j] = seetij
                dfs(i+1, j, seetij)
        if i-1 >= 0:
            if seet[i-1][j] == -1:
                dfsu(i-1, j, seetij)
            if seet[i-1][j] > seetij:
                seet[i-1][j] = seetij
                dfs(i-1, j, seetij)
        if j+1 < N:
            if seet[i][j+1] == -1:
                dfsr(i, j+1, seetij)
            if seet[i][j+1] > seetij:
                seet[i][j+1] = seetij
                dfs(i, j+1, seetij)
        if j-1 >= 0:
            if seet[i][j-1] == -1:
                dfsl(i, j-1, seetij)
            if seet[i][j-1] > seetij:
                seet[i][j-1] = seetij
                dfs(i, j-1, seetij)
        seet[i][j] = -1

    #print(nseet)
    print(ans)


if __name__ == "__main__":
    main()
