import sys,queue,math,copy,itertools,bisect,collections
from heapq import *

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**3
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())
    SI = lambda : sys.stdin.readline().rstrip()

    N = NI()
    P = LI()

    nx = [[0] * N for _ in range(N)]
    ny = [[0] * N for _ in range(N)]
    for i in range(N):
        for j in range(N):
            nx[j][i] = j+1
            ny[i][j] = j+1
    ans = 0
    for a in P:
        x = (a-1) % N
        y = (a-1) // N
        if x == 0 or x == N-1 or y == 0 or y == N-1:
            min_cnt = 0
        else:
            min_cnt = min(nx[x-1][y],nx[-1][y]-nx[x][y],ny[x][y-1],ny[x][-1]-ny[x][y])
            for i in range(1,y):
                min_cnt = min(min_cnt,ny[x][y-1]-ny[x][i-1] + min(nx[x-1][i],nx[-1][i]-nx[x][i]))
            for i in range(y+1,N-1):
                min_cnt = min(min_cnt,ny[x][i]-ny[x][y] + min(nx[x-1][i],nx[-1][i]-nx[x][i]))

            for i in range(1,x):
                min_cnt = min(min_cnt,nx[x-1][y]-nx[i-1][y] + min(ny[i][y-1],ny[i][-1]-ny[i][x]))
            for i in range(x+1,N-1):
                min_cnt = min(min_cnt,nx[i][y]-nx[x][y] + min(ny[i][y-1],ny[i][-1]-ny[i][x]))

        ans += min_cnt

        for i in range(x,N):
            nx[i][y] -= 1
        for i in range(y,N):
            ny[x][i] -= 1
    print(ans)

if __name__ == '__main__':
    main()