import sys
INF = 1 << 60
MOD = 10**9 + 7 # 998244353
sys.setrecursionlimit(2147483647)
input = lambda:sys.stdin.readline().rstrip()
def resolve():
    n = int(input())
    dp = [[0] * n for _ in range(n)]
    used = [[True] * n for _ in range(n)]
    for i in range(n):
        for j in range(n):
            dp[i][j] = min(i, j, n - i - 1, n - j - 1)

    ans = 0
    for p in map(lambda x : int(x) - 1, input().split()):
        i, j = divmod(p, n)
        ans += dp[i][j]
        used[i][j] = False
        # dfs
        stack = [(i, j)]
        D = [(-1, 0), (1, 0), (0, -1), (0, 1)]
        while stack:
            i, j = stack.pop()
            for di, dj in D:
                ni, nj = i + di, j + dj
                if not (0 <= ni < n and 0 <= nj < n):
                    continue
                if dp[ni][nj] > dp[i][j] + used[i][j]:
                    dp[ni][nj] = dp[i][j] + used[i][j]
                    stack.append((ni, nj))

    print(ans)
resolve()