import sys

import numpy as np

int1 = lambda x: int(x) - 1

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(500000)



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

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



ans = pow(10, 11)



for bit in range(1 << (N)):

    if bin(bit).count('1') < K:

        continue

    acc = 0

    pre = 0

    for i in range(N):

        if bit & (1 << i):

            if A[i] <= pre:

                acc += max(0, pre + 1 - A[i])

                pre += 1

        pre = max(pre, A[i])

    ans = min(ans, acc)

print((int(ans)))
import sys

import numpy as np

int1 = lambda x: int(x) - 1

read = sys.stdin.buffer.read

readline = sys.stdin.buffer.readline

readlines = sys.stdin.buffer.readlines

sys.setrecursionlimit(500000)



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

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



ans = pow(10, 11)



for bit in range(1 << (N)):

    if bin(bit).count('1') < K:

        continue

    acc = 0

    pre = A[0] - 1

    for i, a in enumerate(A[:]):

        if bit & (1 << i):

            if pre < a:

                pre = a

            else:

                acc += pre + 1 - a

                pre = pre + 1

        else:

            if pre < a:

                pre = a



    ans = min(ans, acc)

print((int(ans)))

import sys
import numpy as np

int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
N, K = list(map(int, readline().split()))
A = list(map(int, readline().split()))
ans = pow(10, 11)
for bit in range(1 << (N)):
    if bin(bit).count("1") < K:
        continue
    acc = 0
    pre = 0
    for i in range(N):
        if bit & (1 << i):
            if A[i] <= pre:
                acc += max(0, pre + 1 - A[i])
                pre += 1
        pre = max(pre, A[i])
    ans = min(ans, acc)
print((int(ans)))

import sys
import numpy as np

int1 = lambda x: int(x) - 1
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(500000)
N, K = list(map(int, readline().split()))
A = list(map(int, readline().split()))
ans = pow(10, 11)
for bit in range(1 << (N)):
    if bin(bit).count("1") < K:
        continue
    acc = 0
    pre = A[0] - 1
    for i, a in enumerate(A[:]):
        if bit & (1 << i):
            if pre < a:
                pre = a
            else:
                acc += pre + 1 - a
                pre = pre + 1
        else:
            if pre < a:
                pre = a
    ans = min(ans, acc)
print((int(ans)))

-    pre = 0 -    for i in range(N): +    pre = A[0] - 1 +    for i, a in enumerate(A[:]): -            if A[i] <= pre: -                acc += max(0, pre + 1 - A[i]) -                pre += 1 -        pre = max(pre, A[i]) +            if pre < a: +                pre = a +            else: +                acc += pre + 1 - a +                pre = pre + 1 +        else: +            if pre < a: +                pre = a