import sys
from collections import deque
#import numpy

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

digtmp = [[0, 1], [1, 0], [0, -1], [-1, 0]]

def main():
    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
    out = [[0]*N for _ in range(N)]
    for p in P:
        i, j = (p-1)//N, (p-1)%N
        seetij = seet[i][j]
        ans += seetij
        out[i][j] = 1
        que = deque([[i, j]])
        check = [[0]*N for _ in range(N)]

        for k in range(4):
            to = [i+digtmp[k][0], j+digtmp[k][1]]
            if 0 <= to[0] < N and 0 <= to[1] < N:
                if seetij < seet[to[0]][to[1]]:
                    seet[to[0]][to[1]] = seetij
                    que.append(to)

        for _ in range(pow(500,2)+1):
            fr = que.popleft()
            seetfr = seet[fr[0]][fr[1]]
            check[fr[0]][fr[1]] = 1
            status = out[fr[0]][fr[1]]
            for k in range(4):
                to = [fr[0]+digtmp[k][0], fr[1]+digtmp[k][1]]
                if 0 <= to[0] < N and 0 <= to[1] < N:
                    checkto = check[to[0]][to[1]]
                    if seetfr < seet[to[0]][to[1]] and checkto == 0:
                        seet[to[0]][to[1]] = (status == 0)*(seetfr+1) + (status == 1)*seetfr
                        que.append(to)
            if len(que) == 0:
                break

    #print(nseet)
    print(ans)


if __name__ == "__main__":
    main()
