n=int(input())
p=list(map(int,input().split()))
h=[min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)]
ans=0
def func(x):
    # hを更新
    c=h[x]
    h[x]=-1
    todo=[x]
    seen=[0]*(n**2)
    while todo:
        v=todo.pop()
        nvs=[]
        if v+n<n**2:nvs.append(v+n)
        if v%n<n-1 and v+1<n**2:nvs.append(v+1)
        if v-n>=0:nvs.append(v-n)
        if v%n!=0 and v-1>=0:nvs.append(v-1)
        for nv in nvs:
            if seen[nv]==1:continue
            if h[nv]==-1:
                todo.append(nv)
            elif h[nv]>c:
                h[nv]=c
            seen[nv]=1
for x in p:
    ans+=h[x-1]
    func(x-1)
print(ans)