from collections import deque
n = int(input())
p = [int(i)-1 for i in input().split()]
c = [[1]*n for _ in range(n)] 
d = [[1]*n for _ in range(n)]
for x in range(n):
    for y in range(n):
        d[y][x] += min(min(x, n-x-1), min(y, n-y-1))
ans = 0
for i in p:
    x, y = i%n, i//n
    c[y][x] = 0
    ans += d[y][x]-1
    d[y][x] -= 1
    que = deque([[x, y]])
    while que:
        x, y = que.popleft()
        for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
            if 0 <=y+dy< n and 0<=x+dx<n:
                if d[y+dy][x+dx]-d[y][x]==2:
                    d[y+dy][x+dx] -= 1
                    que.append([x+dx, y+dy])
print(ans)