import sys

max_int = 1000000001  # 10^9+1
min_int = -max_int

n = int(sys.stdin.readline())
p = sys.stdin.readline().split()
d = [0] * len(p)

for move_number, pos in enumerate(p, 1):
    d[int(pos) - 1] = move_number

ans = 0
for x in range(n):
    for y in range(n):
        pos = y * n + x
        distances = []
        cntr = 0
        for i in range(x + 1, n):
            if d[pos] < d[y * n + i]:
                cntr += 1
        distances.append(cntr)
        if cntr == 0:
            continue
        # else:


        cntr = 0
        for i in range(x - 1, -1, -1):
            if d[pos] < d[y * n + i]:
                cntr += 1
        distances.append(cntr)
        if cntr == 0:
            continue

        cntr = 0
        for i in range(y + 1, n):
            if d[pos] < d[i * n + x]:
                cntr += 1
        distances.append(cntr)
        if cntr == 0:
            continue

        cntr = 0
        for i in range(y - 1, -1, -1):
            if d[pos] < d[i * n + x]:
                cntr += 1
        distances.append(cntr)
        if cntr == 0:
            continue

        ans += min(distances)

print(ans)
