#import numpy as np
import sys
sys.setrecursionlimit(20000)
[n], *e = [[*map(int, t.split())] for t in open(0)]
#n = 500
# np.random.seed(seed=64)
#ppp = [x + 1 for x in np.random.permutation(n * n).astype(np.int32)]
#e = [ppp]
# print(ppp)


viewers = set(range(1, n**2 + 1))
mem = {x: min((x - 1) % n, (n - x) % n, (x - 1)//n, n - 1 - (x - 1)//n)
       for x in range(1, n**2 + 1)}


def directions(x):
    tmp = []
    if x % n != 1:
        tmp.append(x - 1)
    if x % n > 0:
        tmp.append(x + 1)
    if (x - 1)//n > 0:
        tmp.append(x - n)
    if (x - 1)//n < n - 1:
        tmp.append(x + n)
    return tmp


ans = 0

for x in e[0]:
    visited = set()
    visited.add(x)

    def fill(x):
        queue = []
        for k in directions(x):
            if k not in visited:
                visited.add(k)
                if mem[k] > mem[x] + (x in viewers):
                    mem[k] = mem[x] + (x in viewers)
                    queue.append(k)
        for node in queue:
            fill(node)

    ans += mem[x]
    viewers.remove(x)
    fill(x)

print(ans)
