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)

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