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

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



ans = 1 << 60

for i in range(1 << N):

    if i % 2 == 0:

        continue

    

    selected = [j for j in range(N) if i & (1 << j)]

    if len(selected) != K:

        continue

    

    h_max = a[0]

    cost = 0

    for j in range(1, N):

        if j in selected:

            if a[j] <= h_max:

                cost += h_max + 1 - a[j]

                h_max += 1

            else:

                h_max = a[j]

        else:

            h_max = max(h_max, a[j])

    

    ans = min(ans, cost)



print(ans)
import sys



read = sys.stdin.read

readline = sys.stdin.readline

readlines = sys.stdin.readlines

sys.setrecursionlimit(10 ** 9)

INF = 1 << 60





def main():

    N, K, *A = list(map(int, read().split()))



    ans = INF

    for bit in range(1 << N):

        if (not (bit & 1)) or bin(bit).count('1') != K:

            continue



        total = 0

        max_height = A[0]

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

            if a <= max_height:

                if bit & (1 << i):

                    total += max_height - a + 1

                    max_height += 1

            else:

                max_height = a



        if ans > total:

            ans = total



    print(ans)



    return





if __name__ == '__main__':

    main()

N, K = list(map(int, input().split()))
a = list(map(int, input().split()))
ans = 1 << 60
for i in range(1 << N):
    if i % 2 == 0:
        continue
    selected = [j for j in range(N) if i & (1 << j)]
    if len(selected) != K:
        continue
    h_max = a[0]
    cost = 0
    for j in range(1, N):
        if j in selected:
            if a[j] <= h_max:
                cost += h_max + 1 - a[j]
                h_max += 1
            else:
                h_max = a[j]
        else:
            h_max = max(h_max, a[j])
    ans = min(ans, cost)
print(ans)

import sys

read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10**9)
INF = 1 << 60


def main():
    N, K, *A = list(map(int, read().split()))
    ans = INF
    for bit in range(1 << N):
        if (not (bit & 1)) or bin(bit).count("1") != K:
            continue
        total = 0
        max_height = A[0]
        for i, a in enumerate(A[1:], 1):
            if a <= max_height:
                if bit & (1 << i):
                    total += max_height - a + 1
                    max_height += 1
            else:
                max_height = a
        if ans > total:
            ans = total
    print(ans)
    return


if __name__ == "__main__":
    main()

-N, K = list(map(int, input().split())) -a = list(map(int, input().split())) -ans = 1 << 60 -for i in range(1 << N): -    if i % 2 == 0: -        continue -    selected = [j for j in range(N) if i & (1 << j)] -    if len(selected) != K: -        continue -    h_max = a[0] -    cost = 0 -    for j in range(1, N): -        if j in selected: -            if a[j] <= h_max: -                cost += h_max + 1 - a[j] -                h_max += 1 +import sys + +read = sys.stdin.read +readline = sys.stdin.readline +readlines = sys.stdin.readlines +sys.setrecursionlimit(10**9) +INF = 1 << 60 + + +def main(): +    N, K, *A = list(map(int, read().split())) +    ans = INF +    for bit in range(1 << N): +        if (not (bit & 1)) or bin(bit).count("1") != K: +            continue +        total = 0 +        max_height = A[0] +        for i, a in enumerate(A[1:], 1): +            if a <= max_height: +                if bit & (1 << i): +                    total += max_height - a + 1 +                    max_height += 1 -                h_max = a[j] -        else: -            h_max = max(h_max, a[j]) -    ans = min(ans, cost) -print(ans) +                max_height = a +        if ans > total: +            ans = total +    print(ans) +    return + + +if __name__ == "__main__": +    main()