import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
from collections import deque, defaultdict
def resolve():
    n = int(input())
    ans = 0
    grid = [[1] * n for _ in range(n)]
    D = [(-1, 0), (1, 0), (0, -1), (0, 1)]

    for p in map(lambda x : int(x) - 1, input().split()):
        i, j = divmod(p, n)
        grid[i][j] = 0
        dist = defaultdict(lambda:INF)
        dist[i, j] = 0
        Q = deque([(i, j)])

        while Q:
            i, j = Q.popleft()
            if i in (0, n - 1) or j in (0, n - 1):
                ans += dist[i, j]
                break
            for di, dj in D:
                ni, nj = i + di, j + dj
                if not (0 <= ni < n and 0 <= nj < n):
                    continue
                if dist[ni, nj] > dist[i, j] + grid[ni][nj]:
                    dist[ni, nj] = dist[i, j] + grid[ni][nj]
                    if grid[ni][nj] == 0:
                        Q.appendleft((ni, nj))
                    else:
                        Q.append((ni, nj))

    print(ans)
resolve()