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

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

        min_cnt = INF
        for dd in (((1,0),(0,1)),((1,0),(0,-1)),((-1,0),(0,1)),((-1,0),(0,-1))):
            dp = [[INF] * N for _ in range(N)]
            d = 0
            q = collections.deque()
            q.append((x,y,d))
            dp[x][y] = 0
            while q:
                i,j,d = q.popleft()
                for di,dj in dd:
                    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
                        q.append((i+di,j+dj,d+e))
                else:
                    continue
                break
            min_cnt = min(min_cnt,d)
        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()