def init(n):
    d = {}
    for i in range(n*n):
        x, y = i // n, i % n
        left = i - x * n
        right = (x+1) * n - 1 - i
        top = (i - y) // n
        bottom = (n * (n-1) + y - i) // n
        d[i] = [left, top, right, bottom]
    return d

def update(dic, cur, n):
    x, y = cur // n, cur % n
    left = x * n
    right = (x+1) * n - 1
    top = y
    bottom = n * (n-1) + y
    for i in range(left, cur):
        dic[i][2] -= 1
    for i in range(cur+1, right+1):
        dic[i][0] -= 1
    for i in range(top, cur, n):
        dic[i][3] -= 1
    for i in range(cur+n, bottom, n):
        dic[i][1] -= 1

#[left, up, right, bottom]
N = int(input())
P = list(map(int, input().split()))
d = init(N)
ans = 0
# print(d)
for p in P:
    ans += min(d[p-1])
    update(d, p-1, N)
print(ans)