def main():
    import sys
    from collections import deque
    from copy import deepcopy
    #input = sys.stdin.readline
    n = int(input())
    p = [int(i)-1 for i in input().split()]
    c = [[1]*n for _ in range(n)] 
    d = [[0]*n for _ in range(n)] 
    for x in range(n):
        for y in range(n):
            d[y][x] += min(x, n-x-1, y, n-y-1)
    ans = 0
    for i in p:
        x, y = i%n, i//n
        c[y][x] = 0
        ans += d[y][x]
        que = deque([[x, y]])
        while que:
            x, y = que.popleft()
            for dx, dy in [[1, 0], [0, 1], [-1, 0], [0, -1]]:
                nx, ny = x+dx, y+dy
                if 1<=nx<n-1 and 1<=ny<n-1:
                    if d[y][x]+c[y][x]<d[ny][nx]:
                        d[ny][nx] = d[y][x]+c[y][x]
                        que.append([nx, ny])
    return print(ans)
    
if __name__ == '__main__':
    main()