N = int(input()) B = list(map(int, input().split())) L = [[] for _ in range(max(B)+1)] for i in range(N): L[B[i]].append(i) ans = 0 for i in range(2, len(L)): if len(L[i]) > 0: for j in L[i]: for _ in range(i-1): tmp = L[0].pop() ans += abs(tmp - j) print(ans)