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

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

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

print(ans)
