n = int(input()) s = list(map(int, input().split())) c = 0 for i in reversed(range(n)): if s.index(i+1) == i : s.remove(i+1) else: c += len(s)-s.index(i+1)-1 s1 = s*2 s = s1[s.index(i+1)+1: i-1+s.index(i)] print(c)