import sys
input = sys.stdin.readline

class SegmentTree():
    def __init__(self, n):
        self.n = 2 ** (n - 1).bit_length()
        self.data = [1] * (2 * self.n)

    def build(self):
        for k in reversed(range(self.n - 1)):
            self.data[k] = self.data[k * 2 + 1] + self.data[k * 2 + 2]

    def update(self, k, a):
        k += self.n - 1
        self.data[k] = a

        while k > 0:
            k = (k - 1) // 2
            self.data[k] = self.data[k * 2 + 1] + self.data[k * 2 + 2]

    def query(self, l, r):
        L = l + self.n
        R = r + self.n
        ret = 0
        while L < R:
            if R & 1:
                R -= 1
                ret += self.data[R - 1]
            if L & 1:
                ret += self.data[L - 1]
                L += 1
            L >>= 1
            R >>= 1
        return ret

N = int(input())
Q = list(map(int, input().split()))
X_Seg = [SegmentTree(N) for _ in range(N)]
Y_Seg = [SegmentTree(N) for _ in range(N)]
for i in range(N):
    X_Seg[i].build()
    Y_Seg[i].build()

ans = 0

for k in Q:
    k -= 1
    x, y = k % N, k // N

    X_Seg[y].update(x, 0)
    Y_Seg[x].update(y, 0)

    ret = 10**5
    if x == 0 or x == N - 1 or y == 0 or y == N - 1:
        ret = 0
    else:
        L = X_Seg[y].query(0, x)
        R = X_Seg[y].query(x, N)
        U = Y_Seg[x].query(0, y)
        D = Y_Seg[x].query(y, N)
        ret = min(ret, min([L, R, U, D]))
    ans += ret

print(ans)