[n], *e = [[*map(int, t.split())] for t in open(0)]
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): return [d for d in [
    x - 1, x + 1, x - n, x + n] if 0 < d < n**2 + 1]


ans = 0 

for x in e[0]:

    def fill(x):
        for k in directions(x):
            if mem[k] > mem[x]:
                mem[k] = mem[x]
                if k not in viewers:
                    fill(k)
    ans += mem[x]
    viewers.remove(x)
    fill(x)

print(ans)
