n = int(input())
p = [int(i) - 1 for i in input().split()]
d = [[min(i, n - 1 - i,j, n - 1 - j) + 1 for j in range(n)]
     for i in range(n)]
emp = [[1] * n for _ in range(n)]


def get(i, j):
    if 0 <= i < n and 0 <= j < n:
        return d[i][j]
    else:
        return 0


def app(i, j):
    if 0 <= i < n and 0 <= j < n:
        q.append((i, j))

ans = 0
for pp in p:
    i, j = pp // n, pp % n
    ans += d[i][j] - 1
    emp[i][j] = 0

    q = [(i, j)]
    for qq in q:
        i, j = qq
        new = min(get(i + 1, j), get(i - 1, j),
                  get(i, j + 1), get(i, j - 1)) + emp[i][j]
        if new < d[i][j]:
            d[i][j] = new
            app(i+1, j)
            app(i-1, j)
            app(i, j+1)
            app(i, j-1)
print(ans)
