import sys
def main():
    input = sys.stdin.readline
    n = int(input())
    p = list(map(int,input().split()))
    p = [((i-1)//n,(i-1)%n) for i in p]
    g = [[min(i,j,n-i-1,n-j-1) for j in range(n)] for i in range(n)]
    a = [[True]*n for i in range(n)]
    ans = 0
    for y,x in p:
        ans += g[y][x]
        dq = [(y,x)]
        a[y][x] = 0
        while dq:
            y,x = dq.pop()
            m = g[y][x]
            u = a[y][x]
            if x != 0:
                if g[y][x-1] > m:
                    g[y][x-1] = m+u
                    dq.append((y,x-1))
            if x != n-1:
                if g[y][x+1] > m:
                    g[y][x+1] = m+u
                    dq.append((y,x+1))
            if y != 0:
                if g[y-1][x] > m:
                    g[y-1][x] = m+u
                    dq.append((y-1,x))
            if y != n-1:
                if g[y+1][x] > m:
                    g[y+1][x] = m+u
                    dq.append((y+1,x))
    print(ans)
if __name__ == '__main__':
    main()
