import sys
input = lambda : sys.stdin.readline().rstrip()
sys.setrecursionlimit(max(1000, 10**9))
write = lambda x: sys.stdout.write(x+"\n")


n = int(input())
p = list(map(int, input().split()))

d = [None] * (n**2)
for i in range(n):
    for j in range(n):
        d[i*n+j] = min(i, n-1-i, j, n-1-j)
vacant = [False] * (n**2)

ans = 0
for pos in p:
    pos -= 1
    ans += d[pos]
    vacant[pos] = True
    q = [pos]
    while q:
        pos = q.pop()
        tmp = not vacant[pos]
        for diff in (-1, 1, -n, n):
            ppos = diff+pos
            if ppos<0 or ppos>=n*n:
                continue
            if d[ppos]>d[pos]+tmp:
                d[ppos] = d[pos] + tmp
                q.append(ppos)
#         if pos+n<n*n:
#             ppos = pos + n
#             if d[ppos]>v:
#                 d[ppos] = v
#                 nv = v+1 if not vacant[ppos] else v
#                 q.append((nv, ppos))
#         if pos-n>=0:
#             ppos = pos - n
#             if d[ppos]>v:
#                 d[ppos] = v
#                 nv = v+1 if not vacant[ppos] else v
#                 q.append((nv, ppos))
#         if pos+1<n*n:
#             ppos = pos + 1
#             if d[ppos]>v:
#                 d[ppos] = v
#                 nv = v+1 if not vacant[ppos] else v
#                 q.append((nv, ppos))
#         if pos-1>=0:
#             ppos = pos - 1
#             if d[ppos]>v:
#                 d[ppos] = v
#                 nv = v+1 if not vacant[ppos] else v
#                 q.append((nv, ppos))
print(ans)