import numpy as np
N = int(input())
P = list(map(int, input().split()))
p = np.ones((N, N))
ans = 0
for Pi in P:
    x = (Pi - 1) % N
    y = (Pi - 1) // N
    p[x, y] = 0
    left = sum(p[x, 0:y])
    right = sum(p[x, y:])
    front = sum(p[0:x, y])
    back = sum(p[x:, y])
    ans += min(left, right, front, back)
print(int(ans))
