import numpy as np


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()
nd = np.arange(1, (n * n) + 1).reshape((n, n))
nd_t = nd.transpose()
for i in range(len(p)):
    mod = p[i] % n
    s.add(p[i])
    if mod in end or p[i] in f_l or p[i] in e_l:
        continue
    line = p[i] // n
    l1 = nd[line]
    l2 = nd_t[line]

    right = set(l1[:mod])
    left = set(l1[mod:])
    front = set(l2[:mod])
    rear = set(l2[mod:])
    if s >= right or s >= left or s >= front or s >= rear:
        continue

    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)
