import sys
n,*p = map(int,sys.stdin.read().split())
dist = [min(a,n-1-a,b,n-1-b) for a in range(n) for b in range(n)]

def update(v,nv):
    if dist[nv] > dist[v] + iru[v]:
        q.append(nv)
        dist[nv] = dist[v] + iru[v]

ans = 0
iru = [1]*n*n
for pi in p:
    pi -= 1
    ans += dist[pi]
    iru[pi] = 0
    q = [pi]
    while q:
        v = q.pop()
        if v%n != n-1: update(v,v+1)
        if v%n: update(v,v-1)
        if v >= n: update(v,v-n)
        if v+n < n*n: update(v,v+n)
print(ans)