Colonel has n badges. He wants to give one badge to every of his n soldiers. Each badge has a coolness factor, which shows how much it's owner reached. Coolness factor can be increased by one for the cost of one coin. 

For every pair of soldiers one of them should get a badge with strictly higher factor than the second one. Exact values of their factors aren't important, they just need to have distinct factors. 

Colonel knows, which soldier is supposed to get which badge initially, but there is a problem. Some of badges may have the same factor of coolness. Help him and calculate how much money has to be paid for making all badges have different factors of coolness.


-----Input-----

First line of input consists of one integer n (1 ≤ n ≤ 3000).

Next line consists of n integers a_{i} (1 ≤ a_{i} ≤ n), which stand for coolness factor of each badge.


-----Output-----

Output single integer — minimum amount of coins the colonel has to pay.


-----Examples-----
Input
4
1 3 1 4

Output
1
Input
5
1 2 3 2 5

Output
2


-----Note-----

In first sample test we can increase factor of first badge by 1.

In second sample test we can increase factors of the second and the third badge by 1.

Difficulty: interview

Starter code:

Solutions:
n = int(input())
A = list(map(int, input().split()))
A.sort()
prev = -1
res = 0

for x in A:
    if x > prev:
        prev = x
    else:
        prev = prev + 1
        res += prev - x
print(res)



import math
import sys

n=int(input())
c=list(map(int,input().split()))
c.sort()
ans=0
for i in range(1,n):
    if c[i]<=c[i-1]:
        ans += c[i-1]+1-c[i]
        c[i] = c[i-1]+1
print(ans)
        


#!/usr/bin/env python3
n = int(input())
a = list(map(int,input().split()))
a.sort()
c = 0
for i in range(1,n):
    if a[i] <= a[i-1]:
        c += a[i-1] + 1 - a[i]
        a[i] = a[i-1] + 1
print(c)


n = int(input())

a = list(map(int,input().split()))

a = sorted(a)
c = 0

for i in range(n-1):
	if(a[i] >= a[i+1]):
		x = a[i+1]
		a[i+1] += (a[i]-a[i+1]+1)
		c += a[i]-x+1

print(c)

import sys


def main():
    sys.stdin.readline()
    badges = map(int, sys.stdin.readline().split())

    price = 0
    existing_badges = set()
    for b in sorted(badges):
        while b in existing_badges:
            price += 1
            b += 1
        existing_badges.add(b)  

    print (price)

def __starting_point():
    main()
__starting_point()

n = int(input())
a = sorted(list(map(int, input().split())))

cnt = 0
i = 0
while i < n - 1:
    while a[i] >= a[i + 1]:
        a[i + 1] += 1
        cnt += 1
    i += 1
print(cnt)


n=int(input())
s=list(map(int, input().split()))
t=[0 for i in range(6001)]
for i in range(len(s)):
    t[s[i]]+=1
ans=0
for i in range(6001):
    if t[i]>1:
        ans += t[i]-1
        t[i+1]+=t[i]-1
print(ans)

import sys


n = int(input())
if n == 1:
    print(0)
    return
    
l = list(map(int, input().split(" ")))
l.sort()
min = l[0]
val = 0
for i in range(1, len(l)):
    if l[i] <= l[i-1]:
        val += 1+l[i-1]-l[i]
        l[i]+= 1+l[i-1]-l[i]
print(val)
    
    



n = int(input())
bs = list(map(int, input().split()))

assert(len(bs) == n)

bs = sorted(bs)

last = -1
cost = 0
for b in bs:
    if b <= last:
        diff = last - b
        b += diff + 1
        cost += diff + 1
    last = b

print(cost)


from collections import defaultdict

N = input()
a = input().split(' ')
a = map(int, a)

counts = defaultdict(int)
for e in a:
    counts[e] += 1

cost = 0
cont = True

while cont:
    cont = False
    for e in counts:
        if counts[e] > 1:
            counts[e + 1] += counts[e] - 1
            cost += counts[e] - 1
            counts[e] = 1
            cont = True
            break

print(cost)

n = int(input())
l = list(map(int,input().strip().split()))
d = {}
arr = []
for i in l:
	try:
		d[i]+=1
	except KeyError:
		d[i]=1
		arr.append(i)
arr.sort()
curr = arr[0]-1
ans = 0
for j in arr:
	if curr < j:
		m = d[j]
		ans+=(m*(m-1))//2
		curr = j+m-1
	else:
		m = d[j]
		pay = curr+1-j
		ans+=pay*m + (m*(m-1))//2
		curr = curr+m
print(ans)
		
		
	


#!/usr/bin/env python
# -.- coding: utf-8 -.-

n = int(input())
cool_factors = [int(item) for item in input().strip().split(" ")]
cool_factors.sort()
acc = 0
for i in range(1, len(cool_factors)):
    if cool_factors[i] <= cool_factors[i - 1]:
        diff = cool_factors[i - 1] - cool_factors[i] + 1
        cool_factors[i] = cool_factors[i - 1] + 1
        acc += diff
print(acc)



n = int(input())
arr = list(map(int, input().split()))
count = [0] * 6002
m = 1
for i in range(n):
    count[arr[i]] += 1
    m = max(m, arr[i])
ans = []
for i in range(1, 6002):
    if count[i] == 0:
        ans.append(i)
s = 0
ind = 0
for i in range(1, m + 1):
    if count[i] != 0:
        while count[i] != 1:
            if ans[ind] > i:
                s += ans[ind] - i
                
                count[i] -= 1
            ind += 1
print(s)

n = int(input())
a = list(map(int,input().split()))
a.sort()
c = 0
for i in range(1,n):
    if a[i] > a[i-1]:
        continue
    c  += a[i-1] + 1 - a[i]
    a[i]= a[i-1] + 1
print(c)




n = int(input())
b = set()
a = [int(s) for s in input().split()]
res = 0
for i in a:
    if not i in b:
        b.add(i)
    else:
        while True:
            res+=1
            i+=1
            if not i in b:
                b.add(i)
                break
print(res)


n = int(input())
a = list(map(int, input().split(' ')))
d = [0] * n
for item in a:
    d[item-1] += 1
result = 0
for i in range(n-1):
    dm1 = d[i]-1
    if dm1 < 0:
        continue
    d[i+1] += dm1
    result += dm1
result += d[n-1]*(d[n-1]-1)//2
print(result)

n = int(input())
a = [int(x) for x in input().split()]
a.sort()
res = 0
last_upd = 0
for i in range(1, n):
    if a[i] <= a[i-1]:
        res += a[i-1] + 1 - a[i]
        a[i] = a[i-1] + 1

print(res)


n = int(input())
a = [int(x) for x in input().split()]
a.sort()
z = []
res = 0
for i in range(n-1):
    while a[i+1] <= a[i]:
        a[i+1] += 1
        res += 1
print(res)


n = int(input())
u = []
s = [int(i) for i in input().split()]
s.sort()
s.append(6001)
n+=1
k = 0
for i in range(n-1):
    if s[i] == s[i+1]:
        u.append(i)
    elif u and s[i]+1 < s[i+1]:
        t = s[i]+1
        while t < s[i+1] and u:
            k+=(t-s[u.pop(0)])
            t+=1
print(k)


# coding:utf-8

n = int(input())
nums = list(map(int, input().split(' ')))

t = [0 for i in range(6002)]

ret = 0
for i in nums:
    t[i] += 1
ret = 0
for i in range(len(t)):
    if t[i]>0:
        ret += t[i] - 1
        t[i+1] += t[i] - 1
print(ret)

'''
Input
4
1 3 1 4
Output
1

Input
5
1 2 3 2 5
Output
2
'''

N = int(input())
A = sorted(list([int(x)-1 for x in input().split()]))
A = list([x-A[0] for x in A])
bucket = [0]*6010
for a in A: bucket[a] += 1
ans = 0
for i in range(6000):
    if bucket[i] > 1:
        ans += bucket[i] - 1
        bucket[i+1] += bucket[i] - 1
print(ans)


n=int(input())
a=list(map(int,input().split()))
a.sort()
coins=0
for i in range(n):
    if i-1>=0 and a[i]<=a[i-1]:
        coins+=a[i-1]-a[i]+1
        a[i]=a[i-1]+1
print(coins)


x=int(input())
y = list(map(int, input().split(' ')))
y.sort()
c = y[0]
add = 0
for i in range(1, x):
    if y[i] > c:
        c = y[i]
    else:
        c = c + 1
        add += c - y[i]
print(add)

n = int(input())
a = [int(x) for x in input().split()]
r = 0
d = set()

for x in a:
    while x in d:
        x += 1
        r += 1
    d.add(x)

print(r)

n = int(input())
a = list(map(int, input().split()))
s = sum(a)
a.sort()
for i in range(1, len(a)):
  if a[i-1] >= a[i]:
      a[i] = a[i-1] + 1
print(sum(a) - s)