N, K = list(map(int, input().split()))

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



res = 10 ** 15



for bit in range(2 ** N):

    cnt = 0

    for i in range(N):

        if (bit >> i) & 1:

            cnt += 1



    if cnt < K:

        continue



    cost = 0

    height = A[0]

    if (bit >> 0) & 1:

        for i in range(1, N):

            if (bit >> i) & 1 and height >= A[i]:

                cost += height - A[i] + 1

                height += 1

            elif height < A[i]:

                height = A[i]



        res = min(res, cost)



print(res)
import sys



N, K = list(map(int, input().split()))

B = list(map(int, sys.stdin.readline().rsplit()))



res = 10 ** 18

for i in range(2 ** N):

    cnt = 0

    for j in range(N):

        # print(bin(i), bin(i >> j))

        if (i >> j) & 1:

            cnt += 1

    # print(cnt)

    if cnt != K:

        continue

    

    A = B[::]

    cnt = 0

    maxi = A[0] - 1

    for j in range(N):

        if (i >> j) & 1:

            if A[j] <= maxi:

                cnt += (maxi + 1) - A[j]

                A[j] = maxi + 1



        maxi = max(maxi, A[j])



    res = min(res, cnt)



print(res)

# print(A)

N, K = list(map(int, input().split()))
A = list(map(int, input().split()))
res = 10**15
for bit in range(2**N):
    cnt = 0
    for i in range(N):
        if (bit >> i) & 1:
            cnt += 1
    if cnt < K:
        continue
    cost = 0
    height = A[0]
    if (bit >> 0) & 1:
        for i in range(1, N):
            if (bit >> i) & 1 and height >= A[i]:
                cost += height - A[i] + 1
                height += 1
            elif height < A[i]:
                height = A[i]
        res = min(res, cost)
print(res)

import sys

N, K = list(map(int, input().split()))
B = list(map(int, sys.stdin.readline().rsplit()))
res = 10**18
for i in range(2**N):
    cnt = 0
    for j in range(N):
        # print(bin(i), bin(i >> j))
        if (i >> j) & 1:
            cnt += 1
    # print(cnt)
    if cnt != K:
        continue
    A = B[::]
    cnt = 0
    maxi = A[0] - 1
    for j in range(N):
        if (i >> j) & 1:
            if A[j] <= maxi:
                cnt += (maxi + 1) - A[j]
                A[j] = maxi + 1
        maxi = max(maxi, A[j])
    res = min(res, cnt)
print(res)
# print(A)

+import sys + -A = list(map(int, input().split())) -res = 10**15 -for bit in range(2**N): +B = list(map(int, sys.stdin.readline().rsplit())) +res = 10**18 +for i in range(2**N): -    for i in range(N): -        if (bit >> i) & 1: +    for j in range(N): +        # print(bin(i), bin(i >> j)) +        if (i >> j) & 1: -    if cnt < K: +    # print(cnt) +    if cnt != K: -    cost = 0 -    height = A[0] -    if (bit >> 0) & 1: -        for i in range(1, N): -            if (bit >> i) & 1 and height >= A[i]: -                cost += height - A[i] + 1 -                height += 1 -            elif height < A[i]: -                height = A[i] -        res = min(res, cost) +    A = B[::] +    cnt = 0 +    maxi = A[0] - 1 +    for j in range(N): +        if (i >> j) & 1: +            if A[j] <= maxi: +                cnt += (maxi + 1) - A[j] +                A[j] = maxi + 1 +        maxi = max(maxi, A[j]) +    res = min(res, cnt) +# print(A)