# coding=utf-8
# Author: lee215
get = lambda: int(raw_input().strip())
getl = lambda: raw_input().strip()
gets = lambda: map(int, raw_input().strip().split())
getss = lambda n: [gets() for _ in xrange(n)]

raw_input = input
xrange = range

n = get()
A = gets()


dp = [[n] * n for i in xrange(n)]
for i in xrange(n):
    for j in xrange(n):
        dp[i][j] = min(i, j, n - 1 - i, n - 1 - j)
occ = [[1] * n for i in xrange(n)]

res = 0
for a in A:
    a -= 1
    i, j = a // n, a % n
    res += dp[i][j]
    occ[i][j] = 0
    bfs = [[i, j, dp[i][j]]]

    for x, y, v in bfs:
        v += occ[x][y]
        for i, j in [[x + 1, y], [x - 1, y], [x, y + 1], [x, y - 1]]:
            if not (0 <= i < n and 0 <= j < n): continue
            if dp[i][j] <= v: continue
            dp[i][j] = v
            bfs.append((i, j, v))
print(res)
