class SegmentTree(object):
    def __init__(self, sequence, function, identity):
        N = len(sequence)
        self.length = (1 << (N - 1)).bit_length()
        self.data = [identity] * (self.length << 1)
        self.function = function
        self.identity = identity
        # set values
        for i in range(N):
            self.data[i + self.length - 1] = sequence[i]
        # build
        for i in range(self.length - 2, -1, -1) :
            self.data[i] = self.function(self.data[(i << 1) + 1], self.data[(i << 1) + 2])

    def update(self, idx, x):
        idx += self.length - 1
        self.data[idx] = x
        while idx:
            idx = (idx - 1) >> 1
            self.data[idx] = self.function(self.data[(idx << 1) + 1], self.data[(idx << 1) + 2])

    def query(self, p, q):
        if q <= p:
            return self.identity
        p += self.length - 1
        q += self.length - 2
        res = self.identity
        while q - p > 1:
            if not p & 1:
                res = self.function(res, self.data[p])
            if q & 1:
                res = self.function(res, self.data[q])
                q -= 1
            p >>= 1
            q = (q - 1) >> 1
        return self.function(res, self.data[p]) if p == q else self.function(self.function(res, self.data[p]), self.data[q])

N = int(input())
ones = [1] * N
rows = [SegmentTree(ones, lambda a, b: a + b, 0) for _ in range(N)]
columns = [SegmentTree(ones, lambda a, b: a + b, 0) for _ in range(N)]
total = 0
for p in map(int, input().split()):
    i, j = divmod(p - 1, N)
    total += min((rows[i].query(0, j), rows[i].query(j + 1, N), columns[j].query(0, i), columns[j].query(i + 1, N)))
    rows[i].update(j, 0)
    columns[j].update(i, 0)
print(total)