n = int(input())
p = list(map(int, input().split()))

end = {0, 1}
se = {1, n}
f_l = {i for i in range(1, n+1)}
e_l = {n * n - i for i in range(0, n)}
ans = 0

s = set()
for i in range(len(p)):
    mod = p[i] % n
    if mod in end or p[i] in f_l or p[i] in e_l:
        s.add(p[i])
        continue
    hate = 0
    line = p[i] // n + 1
    right = {p[i] - k for k in range(1, mod)}
    left = {p[i] + k for k in range(1, n-mod+1)}
    front = {p[i] - k * n for k in range(1, line)}
    rear = {p[i] + k * n for k in range(1, abs(line - n) + 1)}
    if s >= right or s >= left or s >= front or s >= rear:
        s.add(p[i])
        continue
    else:
        hate = len(right - s)
        hate = min(hate, len(left - s))
        hate = min(hate, len(front - s))
        hate = min(hate, len(rear - s))
    s.add(p[i])
    ans += hate
print(ans)
