n = gets.to_i a = gets.chomp.split.map(&:to_i) ans = 0 ary = [] n.times do |i| ary[i] = a.count(i+1) end n.times do |i| if ary[i] > 1 surplus = ary[i] - 1 prei = i while surplus > 0 i += 1 while ary[i] != 0 ary[i] = 1 ans += i - prei surplus -= 1 end next end if ary[i] == 0 ary[i] = 1 prei = i i += 1 while ary[i] < 2 ary[i] -= 1 ans += i - prei next end end print ans