n = int(input())
p = list(map(int,input().split()))
c=[[0]*n for i in range(n)]
d=[[0]*n for i in range(n)]
for i in range(n):
    for j in range(n):
        c[i][j]=min(i,j,n-i-1,n-j-1)
        d[i][j]=1

def reload(y,x):
    k=c[y][x]+d[y][x]
    if y>0 and y<n:
        if c[y-1][x]>k:
            c[y-1][x]=k
            reload(y-1,x)
    if y>=0 and y<n-1:
        if c[y+1][x]>k:
            c[y+1][x]=k
            reload(y+1,x)
    if x>0 and x<n:
        if c[y][x-1]>k:
            c[y][x-1]=k
            reload(y,x-1)
    if x>=0 and x<n-1:
        if c[y][x+1]>k:
            c[y][x+1]=k
            reload(y,x+1)

ans = 0
for i in range(n**2):
    y=(p[i]-1)//n
    x=(p[i]-1)%n
    ans += c[y][x]
    d[y][x]=0
    reload(y,x)

print(ans)
