from numba import njit

import numpy as np

@njit

def dfs(v,c,h,stay):

    # hを更新

    x,y=divmod(v,n)

    for dx,dy in ((0,1),(0,-1),(1,0),(-1,0)):

        nx,ny=x+dx,y+dy

        if 0<=nx<n and 0<=ny<n:

            nv=nx*n+ny

            if h[nv]>c:

                h[nv]=c

                dfs(nv,c+stay[nv],h,stay)



@njit

def main(n,p):

    h=[min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)]

    h=np.array(h)

    stay=np.full(n**2,1)

    ans=0



    for v in p:

        c=h[v]

        ans+=c

        stay[v]=0

        dfs(v,c,h,stay)

    print(ans)



    #for i in range(n):

    #    print(h[i*n:(i+1)*n])

if __name__=='__main__':

    n=int(eval(input()))

    p=np.array(list(map(int,input().split())),np.int32)

    p-=1

    main(n,p)

from numba import njit

import numpy as np

@njit

def dfs(v,c,h,stay,todo):

    # hを更新

    todo[0]=v

    now=0

    while now>=0:

        vv=todo[now]

        now-=1

        x,y=divmod(vv,n)

        for dx,dy in ((0,1),(0,-1),(1,0),(-1,0)):

            nx,ny=x+dx,y+dy

            if 0<=nx<n and 0<=ny<n:

                nv=nx*n+ny

                if h[nv]>h[vv]+stay[vv]:

                    h[nv]=h[vv]+stay[vv]

                    now+=1

                    todo[now]=nv



@njit

def main(n,p):

    h=[min(i//n,n-i//n-1,i%n,n-i%n-1) for i in range(n**2)]

    h=np.array(h,np.int32)

    stay=np.full(n*n,1,np.int32)

    todo=np.zeros(n*n,np.int32)

    ans=0

    for v in p:

        c=h[v]

        ans+=c

        stay[v]=0

        dfs(v,c,h,stay,todo)

    print(ans)



if __name__=='__main__':

    n=int(eval(input()))

    p=np.array(list(map(int,input().split())),np.int32)

    p-=1

    main(n,p)
from numba import njit
import numpy as np


@njit
def dfs(v, c, h, stay):
    # hを更新
    x, y = divmod(v, n)
    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)):
        nx, ny = x + dx, y + dy
        if 0 <= nx < n and 0 <= ny < n:
            nv = nx * n + ny
            if h[nv] > c:
                h[nv] = c
                dfs(nv, c + stay[nv], h, stay)


@njit
def main(n, p):
    h = [min(i // n, n - i // n - 1, i % n, n - i % n - 1) for i in range(n**2)]
    h = np.array(h)
    stay = np.full(n**2, 1)
    ans = 0
    for v in p:
        c = h[v]
        ans += c
        stay[v] = 0
        dfs(v, c, h, stay)
    print(ans)
    # for i in range(n):
    #    print(h[i*n:(i+1)*n])


if __name__ == "__main__":
    n = int(eval(input()))
    p = np.array(list(map(int, input().split())), np.int32)
    p -= 1
    main(n, p)

from numba import njit
import numpy as np


@njit
def dfs(v, c, h, stay, todo):
    # hを更新
    todo[0] = v
    now = 0
    while now >= 0:
        vv = todo[now]
        now -= 1
        x, y = divmod(vv, n)
        for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)):
            nx, ny = x + dx, y + dy
            if 0 <= nx < n and 0 <= ny < n:
                nv = nx * n + ny
                if h[nv] > h[vv] + stay[vv]:
                    h[nv] = h[vv] + stay[vv]
                    now += 1
                    todo[now] = nv


@njit
def main(n, p):
    h = [min(i // n, n - i // n - 1, i % n, n - i % n - 1) for i in range(n**2)]
    h = np.array(h, np.int32)
    stay = np.full(n * n, 1, np.int32)
    todo = np.zeros(n * n, np.int32)
    ans = 0
    for v in p:
        c = h[v]
        ans += c
        stay[v] = 0
        dfs(v, c, h, stay, todo)
    print(ans)


if __name__ == "__main__":
    n = int(eval(input()))
    p = np.array(list(map(int, input().split())), np.int32)
    p -= 1
    main(n, p)

-def dfs(v, c, h, stay): +def dfs(v, c, h, stay, todo): -    x, y = divmod(v, n) -    for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)): -        nx, ny = x + dx, y + dy -        if 0 <= nx < n and 0 <= ny < n: -            nv = nx * n + ny -            if h[nv] > c: -                h[nv] = c -                dfs(nv, c + stay[nv], h, stay) +    todo[0] = v +    now = 0 +    while now >= 0: +        vv = todo[now] +        now -= 1 +        x, y = divmod(vv, n) +        for dx, dy in ((0, 1), (0, -1), (1, 0), (-1, 0)): +            nx, ny = x + dx, y + dy +            if 0 <= nx < n and 0 <= ny < n: +                nv = nx * n + ny +                if h[nv] > h[vv] + stay[vv]: +                    h[nv] = h[vv] + stay[vv] +                    now += 1 +                    todo[now] = nv -    h = np.array(h) -    stay = np.full(n**2, 1) +    h = np.array(h, np.int32) +    stay = np.full(n * n, 1, np.int32) +    todo = np.zeros(n * n, np.int32) -        dfs(v, c, h, stay) +        dfs(v, c, h, stay, todo) -    # for i in range(n): -    #    print(h[i*n:(i+1)*n])