n = int(input())
houses = list(map(int, input().split()))
wires = 0
for plaque in range(1, n + 1):
index = houses.index(plaque)
sec_index = list(reversed(houses)).index(plaque)
sec_index += len(houses) - 1 - sec_index * 2
distance = sec_index - index
wires += distance
else:
del plaque, index, sec_index, distance
print(wires)