def main():

    import sys

    input = sys.stdin.readline

    n = int(eval(input()))

    l = list(map(int,input().split()))

    check = [[True]*n for i in range(n)]

    d =  [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)]



    ans = 0

    for t in l:

        t -=1

        x = t//n

        y = t%n

        check[x][y] = False

        ans += d[x][y]

        q = []

        q.append([x,y,d[x][y]])

        while q:

            bx,by,z = q.pop()



            if bx != 0:

                if d[bx-1][by] > z:

                    d[bx-1][by] = z

                    q.append([bx-1,by,z+check[bx-1][by]])

            if bx != n-1:

                if d[bx+1][by] > z:

                    d[bx+1][by] = z

                    q.append([bx+1,by,z+check[bx+1][by]])

            if by != 0:

                if d[bx][by-1] > z:

                    d[bx][by-1] = z

                    q.append([bx,by-1,z+check[bx][by-1]])

            if by != n-1:

                if d[bx][by+1] > z:

                    d[bx][by+1] = z

                    q.append([bx,by+1,z+check[bx][by+1]])  

    print(ans)

if __name__ =="__main__":

    main()
def main():

    import sys

    input = sys.stdin.readline

    n = int(eval(input()))

    l = list(map(int,input().split()))

    check = [[1 for j in range(n)] for i in range(n)]

    d =  [[min(i, n-i-1, j, n-j-1) for j in range(n)] for i in range(n)]



    ans = 0

    for t in l:

        t -=1

        x = t//n

        y = t%n

        check[x][y] = 0

        ans += d[x][y]

        q = []

        q.append((x,y,d[x][y]))

        while q:

            bx,by,z = q.pop()



            if bx != 0:

                if d[bx-1][by] > z:

                    d[bx-1][by] = z

                    q.append((bx-1,by,z+check[bx-1][by]))

            if bx != n-1:

                if d[bx+1][by] > z:

                    d[bx+1][by] = z

                    q.append((bx+1,by,z+check[bx+1][by]))

            if by != 0:

                if d[bx][by-1] > z:

                    d[bx][by-1] = z

                    q.append((bx,by-1,z+check[bx][by-1]))

            if by != n-1:

                if d[bx][by+1] > z:

                    d[bx][by+1] = z

                    q.append((bx,by+1,z+check[bx][by+1]))  

    print(ans)

if __name__ =="__main__":

    main()

def main():
    import sys

    input = sys.stdin.readline
    n = int(eval(input()))
    l = list(map(int, input().split()))
    check = [[True] * n for i in range(n)]
    d = [[min(i, n - i - 1, j, n - j - 1) for j in range(n)] for i in range(n)]
    ans = 0
    for t in l:
        t -= 1
        x = t // n
        y = t % n
        check[x][y] = False
        ans += d[x][y]
        q = []
        q.append([x, y, d[x][y]])
        while q:
            bx, by, z = q.pop()
            if bx != 0:
                if d[bx - 1][by] > z:
                    d[bx - 1][by] = z
                    q.append([bx - 1, by, z + check[bx - 1][by]])
            if bx != n - 1:
                if d[bx + 1][by] > z:
                    d[bx + 1][by] = z
                    q.append([bx + 1, by, z + check[bx + 1][by]])
            if by != 0:
                if d[bx][by - 1] > z:
                    d[bx][by - 1] = z
                    q.append([bx, by - 1, z + check[bx][by - 1]])
            if by != n - 1:
                if d[bx][by + 1] > z:
                    d[bx][by + 1] = z
                    q.append([bx, by + 1, z + check[bx][by + 1]])
    print(ans)


if __name__ == "__main__":
    main()

def main():
    import sys

    input = sys.stdin.readline
    n = int(eval(input()))
    l = list(map(int, input().split()))
    check = [[1 for j in range(n)] for i in range(n)]
    d = [[min(i, n - i - 1, j, n - j - 1) for j in range(n)] for i in range(n)]
    ans = 0
    for t in l:
        t -= 1
        x = t // n
        y = t % n
        check[x][y] = 0
        ans += d[x][y]
        q = []
        q.append((x, y, d[x][y]))
        while q:
            bx, by, z = q.pop()
            if bx != 0:
                if d[bx - 1][by] > z:
                    d[bx - 1][by] = z
                    q.append((bx - 1, by, z + check[bx - 1][by]))
            if bx != n - 1:
                if d[bx + 1][by] > z:
                    d[bx + 1][by] = z
                    q.append((bx + 1, by, z + check[bx + 1][by]))
            if by != 0:
                if d[bx][by - 1] > z:
                    d[bx][by - 1] = z
                    q.append((bx, by - 1, z + check[bx][by - 1]))
            if by != n - 1:
                if d[bx][by + 1] > z:
                    d[bx][by + 1] = z
                    q.append((bx, by + 1, z + check[bx][by + 1]))
    print(ans)


if __name__ == "__main__":
    main()

-    check = [[True] * n for i in range(n)] +    check = [[1 for j in range(n)] for i in range(n)] -        check[x][y] = False +        check[x][y] = 0 -        q.append([x, y, d[x][y]]) +        q.append((x, y, d[x][y])) -                    q.append([bx - 1, by, z + check[bx - 1][by]]) +                    q.append((bx - 1, by, z + check[bx - 1][by])) -                    q.append([bx + 1, by, z + check[bx + 1][by]]) +                    q.append((bx + 1, by, z + check[bx + 1][by])) -                    q.append([bx, by - 1, z + check[bx][by - 1]]) +                    q.append((bx, by - 1, z + check[bx][by - 1])) -                    q.append([bx, by + 1, z + check[bx][by + 1]]) +                    q.append((bx, by + 1, z + check[bx][by + 1]))