N = int(input()) A = [int(x) for x in input().split()] A_dup = [x for x in A if A.count(x) > 1] for a in set(A_dup): A_dup.remove(a) B = list(set(A)) ans = 0 for a in A_dup: if max(B) == len(B): x = max(B) + 1 else: x = min(set(range(1, max(B))) - set(B)) ans += x - a B.append(max(B) + 1) print(ans)