cin = open(0).read().strip().split('\n')
n = int(cin[0])
p = list(map(int, cin[1].split(' ')))

space = [1]*(n*n)

dx = [1, 0, -1, 0]
dy = [0, -1, 0, 1]

dist = [0]*(n*n)
for r in range(n):
    for c in range(n):
        dist[r*n+c] = min(r, (n-1)-r, c, (n-1)-c)

ans = 0
for pi in p:
    row, col = divmod(pi-1, n)
    q = [(row, col)]
    ans += dist[pi-1]
    space[row*n + col] = 0

    while q:
        ph, pw = q.pop()
        for dyi, dxi in zip(dy,dx):
            if 0<=ph+dyi<n and 0<=pw+dxi<n:
                if dist[(ph+dyi)*n+(pw+dxi)] > dist[ph*n+pw]+space[(ph)*n+(pw)]:
                    q.append((ph+dyi,pw+dxi))
                    dist[(ph+dyi)*n+(pw+dxi)] = dist[ph*n+pw]+space[(ph)*n+(pw)]

print(ans)