import sys
from collections import deque

readline = sys.stdin.buffer.readline

def main():
    n = int(readline().rstrip())
    p = list(map(int, readline().split()))
    diag = [-1, 1, n, -n]
    V = (n + 2)**2
    st = [1] * V
    gr = [0] * V
    ans = 0
    for i in range(n):
        for j in range(n):
            gr[i*n + j + n + 1] = min(min(i, j), n-1-max(i, j))
    for v in p:
        v += n
        ans += gr[v]
        st[v] = 0
        c = min([gr[v+dv] + st[v+dv] for dv in diag])
        if c < gr[v]:
            gr[v] = c
        q = deque()
        q.append(v)
        while q:
            v = q.popleft()
            for dv in diag:
                nv = v + dv
                if gr[nv] > gr[v] + st[v]:
                    gr[nv] = gr[v] + st[v]
                    if st[v] > 0:
                        q.append(nv)
                    else:
                        q.appendleft(nv)
    print(ans)
    return

if __name__ == '__main__':
    main()
