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()

    seat = [[1] * N for _ in range(N)]
    zero = [[1] * N for _ in range(N)]

    ans = 0

    for i in P:
        x = (i-1) % N
        y = (i-1) // N

        dp = [[INF] * N for _ in range(N)]
        d = 0
        q = []
        heapify(q)
        heappush(q,(d,x,y))
        dp[x][y] = 0
        min_cnt = INF
        while q:
            d,i,j = heappop(q)
            for di,dj in ((1,0),(0,1),(-1,0),(0,-1)):
                if not(0 <= i+di < N and 0 <= j+dj < N):
                    break
                if zero[i+di][j+dj] == 0:
                    break
                e = seat[i+di][j+dj]
                if dp[i+di][j+dj] > d+e:
                    dp[i+di][j+dj] = d+e
                    heappush(q,(d+e,i+di,j+dj))
            else:
                continue
            min_cnt = d
            break

        seat[x][y] = 0
        if min_cnt == 0:
            # q = collections.deque()
            # q.append((x,y))
            zero[x][y] = 0
            # while q:
            #     i,j = q.popleft()
            #     for di,dj in ((1,0),(0,1),(-1,0),(0,-1)):
            #         if not(0 <= i+di < N and 0 <= j+dj < N): continue
            #         if seat[i+di][j+dj] == 1 or zero[i+di][j+dj] == 0: continue
            #         zero[i+di][j+dj] = 0
            #         q.append((i+di,j+dj))
        ans += min_cnt
    print(ans)

if __name__ == '__main__':
    main()