There are N buildings along a line, each colored in a unique color and with a specific height. Chokudai, the city mayor, wants to see at least K buildings from the left. The objective is to calculate the minimum cost of increasing the height of the buildings to satisfy this condition. The cost of increasing the height of a building by 1 meter is 1 yen. The height of a building cannot be decreased. The input consists of N, K, and the heights of the buildings. The output should display the minimum cost. Constraints include 1 ≤ K ≤ N ≤ 15 and 1 ≤ ai ≤ 10^9. The problem has different scoring subtasks based on additional constraints. Two sample inputs with their respective optimal solutions are provided.

# Buggy version:
n, k = map(int, input().split())
a = list(map(int, input().split()))

ans = float('inf')
for bit in range(1<<(n-1)):
  cost = 0
  cand = [0]*(n-1)
  for i in range(n-1):
    if bit & 1<<i: cand[i] = 1
  if cand.count(1) != k-1: continue
  mx = a[0]
  for i in range(1, n):
    if cand[i-1]:
      if a[i] <= mx:
        cost += prev-a[i]+1
        ms += 1
    mx = max(mx, a[j])
  ans = min(ans, cost)
print(ans)

# Fixed version:
n, k = map(int, input().split())
a = list(map(int, input().split()))

ans = float('inf')
for bit in range(1<<(n-1)):
  cand = [0]*(n-1)
  for i in range(n-1):
    if bit & 1<<i: cand[i] = 1
  if cand.count(1) != k-1: continue
  cost = 0
  mx = a[0]
  for i in range(1, n):
    if cand[i-1]:
      if a[i] <= mx:
        cost += mx-a[i]+1
        mx += 1
    mx = max(mx, a[i])
  ans = min(ans, cost)
print(ans)

# Buggy version:
n,k = map(int,input().split())
a = list(map(int,input().split()))
# 小課題１
# cost = 0
# for i in range(1,n):
#    if a[i] < a[i-1] + 1 :
#        cost += a[i-1] + 1 - a[i]
#        a[i] = a[i-1] + 1
# print(cost)

ans = 10**100
for i in range(1,2**n,2):
    if k != sum([ i>>j & 1 for j in range(n)]) :
        continue
    kijun = a[0]
    cost = 0
    for j in range(1,n):
        if ((i>>j) & 1) :
            if a[j] <= kijun :
                cost += (kijun + 1 - a[j])
                kijun = a[j] + cost
            else :
                kijun = a[j]
    ans = min(ans,cost)
print(ans)


# Fixed version:
n,k = map(int,input().split())
a = list(map(int,input().split()))
# 小課題１
# cost = 0
# for i in range(1,n):
#    if a[i] < a[i-1] + 1 :
#        cost += a[i-1] + 1 - a[i]
#        a[i] = a[i-1] + 1
# print(cost)

ans = 10**100
for i in range(1,2**n,2):
    if k != sum([ i>>j & 1 for j in range(n)]) :
        continue
    kijun = a[0]
    cost = 0
    for j in range(1,n):
        if ((i>>j) & 1) :
            if a[j] <= kijun :
                cost += (kijun + 1 - a[j])
                kijun = kijun + 1
            else :
                kijun = a[j]
        else :
            kijun = max(kijun,a[j])
    ans = min(ans,cost)
print(ans)

# Buggy version:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(1,N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            diff = max(max(copy_A[:i]) - copy_A[i], 0) # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
            copy_A[i] += diff + 1
            cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)


# Fixed version:
import itertools

N, K = map(int, input().split()) # Nは建物の数、K色以上に塗る
A = list(map(int, input().split()))
N_index = list(range(1,N))

min_cost = float('inf')
for target_indexes in itertools.combinations(N_index, K-1): # N_indexからK個選ぶ方法を全通り試す
    cost = 0
    copy_A = A.copy()
    for i in range(1, N):
        if i in target_indexes:
            if copy_A[i] <= max(copy_A[:i]):
                diff = max(copy_A[:i]) - copy_A[i] # ターゲットの建物の場合はそれより左側の建物のmaxより1だけ大きくする
                copy_A[i] += diff + 1
                cost += diff + 1

    min_cost = min(min_cost, cost)

print(min_cost)

# Buggy version:
from itertools import combinations

n,k=map(int,input().split())
lst=list(map(int,input().split()))

work=lst[0]
ini=[x-work for x in lst]
ini_seen=[0]
work=0
for i in range(1,n):
    if ini[i]>work:
        work=ini[i]
        ini_seen.append(i)

mn=None
for x in combinations(range(n-len(ini_seen)),k-len(ini_seen)):
    sm=0
    work=0
    xtoken=0
    count=1
    for i in range(1,n):
        print(i,work,sm)
        if i in ini_seen:
            count+=1
            if work<ini[i] : work=ini[i]
            else:
                work+=1
                sm+=work-ini[i]
        elif xtoken<len(x) and i==x[xtoken]+count:
            work+=1
            sm+=work-ini[i]
            xtoken+=1
    if not mn : mn=sm
    if mn>sm : mn=sm
print(mn)


# Fixed version:
from itertools import combinations

n,k=map(int,input().split())
lst=list(map(int,input().split()))

work=lst[0]
ini=[x-work for x in lst]
ini_seen=[0]
work=0
for i in range(1,n):
    if ini[i]>work:
        work=ini[i]
        ini_seen.append(i)

mn=None
for x in combinations(range(n-len(ini_seen)),k-len(ini_seen)):
    sm=0
    work=0
    xtoken=0
    count=1
    for i in range(1,n):
        if i in ini_seen:
            count+=1
            if work<ini[i] : work=ini[i]
            else:
                work+=1
                sm+=work-ini[i]
        elif xtoken<len(x) and i==x[xtoken]+count:
            work+=1
            sm+=work-ini[i]
            xtoken+=1
    if not mn : mn=sm
    if mn>sm : mn=sm
print(mn)

# Buggy version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=10010010010010010

for i in range(1<<n-1):
  if bin(i).count("1")!=k-1:
    continue
  cnt=0
  cost=0
  print("cnt i",i)
  prevh=a[0]
  for j in range(n-1):
    if 1<<j & i:
      if prevh>=a[j+1]:
        cost+=prevh+1-a[j+1]
        prevh+=1
        print("cost",j,cost)
      else:
        prevh=a[j+1]
  ans=min(ans,cost)
  #print("ans",ans)
print(ans)

# Fixed version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=10010010010010010

for i in range(1<<n-1):
  if bin(i).count("1")!=k-1:
    continue
  cnt=0
  cost=0
  #print("cnt i",i)
  prevh=a[0]
  for j in range(n-1):
    if 1<<j & i:
      if prevh>=a[j+1]:
        cost+=prevh+1-a[j+1]
        prevh+=1
        #print("cost",j,cost)
      else:
        prevh=a[j+1]
    else:
      prevh=max(prevh,a[j+1])
  ans=min(ans,cost)
  #print("ans",ans)
print(ans)

# Buggy version:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
#n = int(input())
n, k = map(int, sys.stdin.readline().split())
#n = input()
a = list(map(int, sys.stdin.readline().split()))

l = list(itertools.combinations(range(n), k))
print(a)
print(l)

ans = 10 ** 18
for i in range(len(l)):
    cost = 0
    m1 = 0
    p = 20
    for j in range(l[i][0]+1):
        if a[j] > m1:
            m1 = a[j]
            p = j

        if j == l[i][0]:
            if m1 == a[j]:
                if p != j:
                    cost += 1
            else:
                cost += (m1 - a[j] + 1)

    
    r = 1
    if len(l[0]) >= 2:
        q = l[i][r]
    else:
        q = 20

    m2 = a[l[i][0]] + cost

    for j in range(l[i][0]+1, n):
        if j == q:
            if a[j] > m2:
                m2 = a[j]
            else:
                m2 += 1
                cost += (m2 - a[j])

            if r != len(l[0])-1:
                r += 1
                q = l[i][r]
            
        else:
            if a[j] > m2:
                cost += a[j] - m2
                m2 = a[j]
                

    if cost < ans:
        ans = cost


print(ans)
                
        



















# Fixed version:
import sys
import math
import collections
import decimal
import itertools
from collections import deque
from functools import reduce
import heapq
#n = int(input())
n, k = map(int, sys.stdin.readline().split())
#n = input()
a = list(map(int, sys.stdin.readline().split()))

l = list(itertools.combinations(range(n), k))

ans = 10 ** 18
for i in range(len(l)):
    cost = 0
    m1 = 0
    p = 20
    for j in range(l[i][0]+1):
        if a[j] > m1:
            m1 = a[j]
            p = j

        if j == l[i][0]:
            if m1 == a[j]:
                if p != j:
                    cost += 1
            else:
                cost += (m1 - a[j] + 1)

    
    r = 1
    if len(l[0]) >= 2:
        q = l[i][r]
    else:
        q = 20

    m2 = a[l[i][0]] + cost

    for j in range(l[i][0]+1, n):
        if j == q:
            if a[j] > m2:
                m2 = a[j]
            else:
                m2 += 1
                cost += (m2 - a[j])

            if r != len(l[0])-1:
                r += 1
                q = l[i][r]
            
        else:
            if a[j] > m2:
                cost += a[j] - m2
                m2 = a[j]
                

    if cost < ans:
        ans = cost


print(ans)

# Buggy version:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
# if N==K:
#     cost=0
#     for i in range(1,N):
#         if height2[i-1]>=height2[i]:
#             cost+=height2[i-1]-height2[i]+1
#             height2[i]+=height2[i-1]-height2[i]+1
#     print(cost)
for i in range(2 ** N):
    count=1
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")>=K:
        for j in range(N):
            if i >> j & 1:
                if j!=0:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]+=tarou-height2[j]+1

        if ans > cost
            ans = cost

print(ans)

# Fixed version:
N, K = list(map(int, input().split()))
height = list(map(int, input().split()))
height2=height.copy()
ans = 10 ** 10
if N==K:
    cost=0
    for i in range(1,N):
        tarou=max(height2[:i])
        if tarou>=height2[i]:
            cost+=tarou-height2[i]+1
            height2[i]=tarou+1
    print(cost)
    exit()
for i in range(2 ** N):
    cost = 0
    height2 = height.copy()
    if bin(i).count("1")==K:
        for j in range(1,N):
            if i >> j & 1:
                if N==K:
                    if height2[j-1]>=height2[j]:
                            cost+=height2[j-1]-height2[j]+1
                            height2[j]+=height2[j-1]-height2[j]+1
                else:
                    tarou=max(height2[:j])
                    if height2[j]<=tarou:
                        cost+=tarou-height2[j]+1
                        height2[j]=tarou+1

        if ans > cost:
            ans = cost

print(ans)

# Buggy version:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

from itertools import combinations

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

minc=10**12
for i in combinations(a,K):
    cost=0
    for j in range(1,K):
        costt=max(i[j-1]-i[j]+1,0)
        i[j]+=costt
        cost+=costt
    minc=min(cost,minc)

print(minc)


# Fixed version:
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b

from itertools import combinations

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

minc=10**12
ai=[i for i in range(N)]
for i in combinations(ai,K):
    ac=a[:]
    cost=0
    i=list(i)
    for j in range(1,K):
        costt=max(max(ac[:i[j]])-ac[i[j]]+1,0)
        ac[i[j]]+=costt
        cost+=costt
    minc=min(cost,minc)
print(minc)

# Buggy version:
n , k = map(int, input().split())
x = list(map(int,input().split()))
b = [list(map(int,bin(x)[2:].zfill(n))) for x in range(2**n)]
h = [0]*n

def check(li,n,k):
    c=1
    hi=li[0]
    for i in range(1,n):
        if li[i]>hi:
            c+=1
            hi=li[i]
    if c>=k:
        return True
    else:
        return False

for i in range(1,n):
    h[i]=max(h[i-1],x[i-1])

mi = float('inf')
for bit in b:
    ans = 0
    s1=0
    x2=x.copy()
    if bit[0]==1:
        for i in range(n):
            if bit[i]==1:
                s2=x[i]
                if s2<=s1:
                    ans+=s1-s2+1
                    s2=s1+1
                if s2<=h[i]:
                    ans += h[i]-s2+1
                    s2=h[i]+1
                s1=s2
                x2[i]=s2
            else:
                x2[i]=x[i]
    if check(x2,n,k):
        mi=min(mi,ans)
        print(x2,ans)

print(mi)


# Fixed version:
n , k = map(int, input().split())
x = list(map(int,input().split()))
b = [list(map(int,bin(x)[2:].zfill(n))) for x in range(2**n)]
h = [0]*n

def check(li,n,k):
    c=1
    hi=li[0]
    for i in range(1,n):
        if li[i]>hi:
            c+=1
            hi=li[i]
    if c>=k:
        return True
    else:
        return False

for i in range(1,n):
    h[i]=max(h[i-1],x[i-1])

mi = float('inf')
for bit in b:
    ans = 0
    s1=0
    x2=x.copy()
    if bit[0]==1:
        for i in range(n):
            if bit[i]==1:
                s2=x[i]
                if s2<=s1:
                    ans+=s1-s2+1
                    s2=s1+1
                if s2<=h[i]:
                    ans += h[i]-s2+1
                    s2=h[i]+1
                s1=s2
                x2[i]=s2
            else:
                x2[i]=x[i]
    if check(x2,n,k):
        mi=min(mi,ans)

print(mi)

# Buggy version:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n,k=n1()
a=n1()

import itertools
ite=itertools.combinations(range(n),k)

ans=10**20
for line in ite:
    c=0
    b=a.copy()
    for i in range(1,k):
        if b[line[i]]<=b[line[i-1]]:
            plus=b[line[i-1]]-b[line[i]]+1
            b[line[i]]+=plus
            c+=plus
    ok=1
    for i in range(n):
        if b[i-1]<b[i]:
            ok+=1
        else:
            b[i]=b[i-1]
    if ok>=3:
        ans=min(ans,c)
print(ans)

# Fixed version:
def n0():return int(input())
def n1():return [int(x) for x in input().split()]
def n2(n):return [int(input()) for _ in range(n)]
def n3(n):return [[int(x) for x in input().split()] for _ in range(n)]

n,k=n1()
a=n1()

import itertools
ite=itertools.combinations(range(n),k)

ans=10**20
for line in ite:
    c=0
    b=a.copy()
    for i in range(1,k):
        if b[line[i]]<=b[line[i-1]]:
            plus=b[line[i-1]]-b[line[i]]+1
            b[line[i]]+=plus
            c+=plus
    ok=1
    for i in range(1,n):
        if b[i-1]<b[i]:
            ok+=1
        else:
            b[i]=b[i-1]
    if ok>=k:
        ans=min(ans,c)
print(ans)

# Buggy version:
# Square869120Contest #4 B - Buildings are Colorful!　
# 全探探索　ver

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))

ans = float('inf')
for i in range(2 ** (N - 1)):
    bit = [(i >> j) & 1 for j in range(N - 1)]  # 左から2番目以降で見えるようにするビルを選択
    
    can_see = 0    
    cost = 0
    height = a[0]
    for j, b in enumerate(bit):
        if (b == 1) & (height >= a[j+1]):
            cost += height - a[j + 1] + 1
            height = height + 1
            can_see += 1
        elif b == 1:
            height = a[j + 1]
            can_see += 1
        elif height < a[j + 1]:
            can_see += 1
        heigt = max(height, a[j + 1])
    
    if can_see >= K:
        ans = min(ans, cost)

print(ans)







# Fixed version:
# Square869120Contest #4 B - Buildings are Colorful!　
# 全探探索　ver

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))

ans = float('inf')
for i in range(2 ** (N - 1)):
    bit = [(i >> j) & 1 for j in range(N - 1)]  # 左から2番目以降で見えるようにするビルを選択

    can_see = 0
    cost = 0
    height = a[0]
    for j, b in enumerate(bit):
        if (b == 1) & (height >= a[j + 1]):
            cost += height - a[j + 1] + 1
            height = height + 1
            can_see += 1
        elif (b == 1) & (height < a[j + 1]):
            height = a[j + 1]
            can_see += 1
        elif height < a[j + 1]:
            can_see += 1
            height = a[j + 1]

    if can_see >= K - 1:
        ans = min(ans, cost)

print(ans)

# Buggy version:
# Square869120Contest #4 B - Buildings are Colorful!　
 
import sys
 
N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))

if K == 0:
  break
 
def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    if can_see >= target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost
 
    # 端まで探索したとき
    if i == len(a):
        return float('inf')
 
    # 建物 i を選ばない場合
    if height >= a[i]:
        cost1 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    else:
        cost1 = dfs(height=a[i], cost=cost, target=K, can_see=can_see+1, i=i+1)
    # print('cost1', cost1, 'can_see', can_see, 'i', i)
 
    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    height = max(a[i], height + 1)
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see+1, i=i+1)
    # print('cost2', cost2)
 
    return min(cost1, cost2)
 
 
print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

# Fixed version:
# Square869120Contest #4 B - Buildings are Colorful!　

import sys

N, K = map(int, sys.stdin.readline().strip().split())
a = list(map(int, sys.stdin.readline().strip().split()))


def dfs(height=a[0], cost=0, target=K, can_see=1, i=1):
    if can_see >= target:
        # print('height', height)
        # print('can_see', can_see)
        # print('cost', cost)
        return cost

    # 端まで探索したとき
    if i == len(a):
        return float('inf')

    # 建物 i を選ばない場合
    if height >= a[i]:
        cost1 = dfs(height=height, cost=cost, target=K, can_see=can_see, i=i+1)
    else:
        cost1 = dfs(height=a[i], cost=cost, target=K, can_see=can_see+1, i=i+1)
    # print('cost1', cost1, 'can_see', can_see, 'i', i)

    # 建物　i を選んだ場合
    cost += max(0, height + 1 - a[i])
    height = max(a[i], height + 1)
    cost2 = dfs(height=height, cost=cost, target=K, can_see=can_see+1, i=i+1)
    # print('cost2', cost2)

    return min(cost1, cost2)


print(dfs(a[0], cost=0, target=K, can_see=1, i=1))

# Buggy version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=float("inf")
for i in range(2**n):
    bit=[(i>>j)&1 for j in range(n)]
    if sum(bit)==k and bit[0]:
        cost,kijun=0,a[0]
        for j in range(n):
            if not bit[j]:
                kijun=max(kijun,a[k+1])
            elif a[k+1]>=kijun+1:
                kijun=a[k+1]
            else:
                cost+=kijun+1-a[k+1]
                kijun+=1
        ans=min(ans,cost)
print(ans)

# Fixed version:
n,k=map(int,input().split())
a=list(map(int,input().split()))
ans=float("inf")
for i in range(2**n):
    bit=[(i>>j)&1 for j in range(n)]
    if sum(bit)==k and bit[0]==1:
        cost=0
        kijun=a[0]
        for j in range(1,n):
            if bit[j]==0:
                kijun=max(kijun,a[j])
            elif a[j]>=kijun+1:
                kijun=a[j]
            else:
                cost+=kijun+1-a[j]
                kijun+=1
        ans=min(ans,cost)
print(ans)

# Buggy version:
def main():
    import copy
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10**200
    
    for i in range(0, 2 ** n, 2):
        test_lis = copy.deepcopy(takasa_lis)
        print(test_lis)
        cnt = 0
        building_cost = 0
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(max(test_lis[0:j]) - test_lis[j] + 1, 0)
                test_lis[j] = max(max(test_lis[0:j])+1, test_lis[j])
            else:
                if test_lis[j] > max(test_lis[0:j]):
                    cnt += 1
        print(test_lis,cnt, building_cost)
        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()


# Fixed version:
def main():
    import copy
    n, k  = map(int, input().split())
    takasa_lis = list(map(int, input().split()))
   
    ans = 10**200
    
    for i in range(0, 2 ** n, 2):
        test_lis = copy.deepcopy(takasa_lis)
        cnt = 0
        building_cost = 0
        for j in range(1, n):
            if ((i>>j) & 1):
                cnt += 1
                building_cost += max(max(test_lis[0:j]) - test_lis[j] + 1, 0)
                test_lis[j] = max(max(test_lis[0:j])+1, test_lis[j])
            else:
                if test_lis[j] > max(test_lis[0:j]):
                    cnt += 1
        if cnt >= k-1 and ans > building_cost:
            ans = building_cost
    
    print(ans)


    


if __name__ == "__main__":
    main()

# Buggy version:
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=sum(bit):
        continue
    print("bit",bit)
    cost,kijun = 0,a[0]
    for k in range(N-1):
        if bit[k]==0:
            kijun = max(kijun,a[k+1])
            continue
        if a[k+1]>=kijun+1:
            kijun = a[k+1]
            continue
        cost += (kijun+1)-a[k+1]
        kijun += 1
    ans = min(ans,cost)
print(ans)

# Fixed version:
def LI(): return list(map(int,input().split()))
N,K = LI()
a = LI()
ans = float('INF')
for i in range(2**(N-1)):
    bit = [i>>j&1 for j in range(N-1)]
    if K-1!=sum(bit):
        continue
    temp,kijun = 0,a[0]
    for k in range(N-1):
        if bit[k]==0:
            kijun = max(kijun,a[k+1])
        else:
            if a[k+1]>=kijun+1:
                kijun = a[k+1]
                continue
            temp += (kijun+1)-a[k+1]
            kijun += 1
    ans = min(ans,temp)
print(ans)

# Buggy version:
n, k = map(int, input().split())
li = list(map(int, input().split()))
min_ = 10**12
for i in range(1<<n):
    max_height = li[0]
    money = 0
    can_look = 0
    li_h = []
    for j in range(n):
        if j != 0:
            max_height = max(max_height, li_h[j-1])
        #j番目が見えるようにする
        if j == 0:
            can_look += 1
            li_h.append(li[0])
            continue
        if (i>>j)&1:
            can_look += 1
            if li[j] <= max_height:
                li_h.append(max_height+1)
            else:
                li_h.append(li[j])
        else:
            li_h.append(li[j])
    if can_look >= k:
        money = sum(li_h) - sum(li)
        min_ = min(min_, money)
        print(li_h)
        print(li)
        
print(min_)

# Fixed version:
n, k = map(int, input().split())
li = list(map(int, input().split()))
min_ = 10**12
for i in range(1<<n):
    max_height = li[0]
    money = 0
    can_look = 0
    li_h = []
    for j in range(n):
        if j != 0:
            max_height = max(max_height, li_h[j-1])
        #j番目が見えるようにする
        if j == 0:
            can_look += 1
            li_h.append(li[0])
            continue
        if (i>>j)&1:
            can_look += 1
            if li[j] <= max_height:
                li_h.append(max_height+1)
            else:
                li_h.append(li[j])
        else:
            li_h.append(li[j])
    if can_look >= k:
        money = sum(li_h) - sum(li)
        min_ = min(min_, money)

print(min_)

# Buggy version:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = 10**30
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        print(pattern)
        for i in pattern:
            height += 1
            if A[i] >= height:
                height = A[i]
                continue
            tmp += height - A[i]
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()


# Fixed version:
from itertools import combinations
import sys
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines

def main():
    N,K,*A = map(int, read().split())

    ans = float('inf')
    for pattern in combinations(range(1,N), K-1):
        height = A[0]
        tmp = 0
        for i in range(1,N):
            if i in pattern:
                height += 1
                if A[i] >= height:
                    height = A[i]
                    continue
                tmp += height - A[i]
            height = max(height, A[i])
        ans = min(ans, tmp)
    
    print(ans)


if __name__ == "__main__":
    main()

# Buggy version:
# square869120Contest #4
# B - Buildings are Colorful!
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
N, K = map(int, input().split())
*A, = map(int, input().split())

ans = float('inf')
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
    maxh = 0
    for i, a in enumerate(A):
        if maxh < a:
            maxh = a
        if bit >> i & 1 and maxh >= a:
            cost += maxh - a + 1
            maxh += 1

    if cost < ans:
        ans = cost

print(ans)


# Fixed version:
# square869120Contest #4
# B - Buildings are Colorful!
# https://atcoder.jp/contests/s8pc-4/tasks/s8pc_4_b
N, K = map(int, input().split())
*A, = map(int, input().split())

ans = float('inf')
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
    maxh = 0
    for i, a in enumerate(A):
        if bit >> i & 1 and maxh >= a:
            cost += maxh - a + 1
            maxh += 1
        elif maxh < a:
            maxh = a


    if cost < ans:
        ans = cost


print(ans)

# Buggy version:
n,k = map(int,input().split())
a = list(map(int,input().split()))

ans = 1145141919810364364334
for i in range(2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
            print(ls,qq)
    
    cost = 0
    if sum(ls) < k or ls[0] == 0:
        continue
        
    else:
        t = []
        for j in qq:
            t.append(a[j])
        
        mt = t[0]
        cost = 0
        for j in range(len(qq) - 1) :
            if t[j+1] <= mt:
                cost += (mt + 1 - t[j+1])
                mt += 1
            else:
                mt = max(mt,t[j+1])
                
    ans = min(ans,cost)
    
print(ans)

# Fixed version:
n,k = map(int,input().split())
a = list(map(int,input().split()))

ans = 1145141919810364364334
for i in range(1,2**n):
    ls = [0] * n
    qq = []
    
    for j in range(n):
        if (i >> j) & 1:
            ls[j] = 1
            qq.append(j)
            
    if sum(ls) < k:
        continue
        
    else:
        mt = a[0]
        cost = 0
        for j in range(1,n) :
            if mt < a[j]:
                mt = a[j]
            else:
                if ls[j] == 1:
                    cost += mt + 1 - a[j]
                    mt += 1
    ans = min(ans,cost)
    
print(ans)

# Buggy version:
import itertools
N, K = list(map(int, input().split()))
A_orig = list(map(int, input().split()))
 
res = len(A_orig) * max(A_orig)
for pattern in itertools.combinations(range(N), K):
    cnt = 0
    A = A_orig[:]
    for p in pattern:
        if p == 0:
            continue
        extension = max(0, max(A[:p]) - A[p] + 1)
        A[p] += extension
        cnt += extension
    res = min(res, cnt)

# Fixed version:
import itertools
N, K = list(map(int, input().split()))
A_orig = list(map(int, input().split()))

res = len(A_orig) * max(A_orig)
for pattern in itertools.combinations(range(N), K):
    cnt = 0
    A = A_orig[:]
    for p in pattern:
        if p == 0:
            continue
        extension = max(0, max(A[:p]) - A[p] + 1)
        A[p] += extension
        cnt += extension
    res = min(res, cnt)
    
print(res)

# Buggy version:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces
nl = lambda: list(map(int, stdin.readline().split()))

n, k = na()
a = na()
ans = float('inf')
for i in range(2**(n-1)):
    colors = 1
    cost = 0
    tmp = a[0]
    for k in range(n-1):
        if tmp < a[k+1]:
            colors += 1
            tmp = a[k+1]
            continue
        if (i >> k) & 1:
            cost += tmp - a[k+1] + 1
            colors += 1
            tmp += 1
    if colors >= k:
        ans = min(ans, cost)
print(ans)


# Fixed version:
import sys

stdin = sys.stdin

ni = lambda: int(ns())
na = lambda: list(map(int, stdin.readline().split()))
ns = lambda: stdin.readline().rstrip()  # ignore trailing spaces
nl = lambda: list(map(int, stdin.readline().split()))

n, K = na()
a = na()
ans = float('inf')
for i in range(2**(n-1)):
    colors = 1
    cost = 0
    tmp = a[0]
    for k in range(n-1):
        if tmp < a[k+1]:
            colors += 1
            tmp = a[k+1]
            continue
        if (i >> k) & 1:
            cost += tmp - a[k+1] + 1
            colors += 1
            tmp += 1
    if colors >= K:
        ans = min(ans, cost)
print(ans)

# Buggy version:
def dfs(index):
    global ans
    if index == N:
        if blst.count(1) == K - 1:
            tmp = a[0]
            anstmp = 0
            for i in range(1, N):
                if blst[i - 1] == 0:continue
                if tmp >= a[i]:
                    anstmp += tmp - a[i] + 1
                    tmp += 1
                else:
                    tmp = a[i]
            ans = min(ans, anstmp)
        return
    for i in range(2):
        blst[index - 1] = i
        dfs(index + 1)
    return

N, K = map(int, input().split())
a = list(map(int, input().split()))
blst = [0] * (N - 1)
ans = float('inf')
dfs(1)

print(0)

# Fixed version:
def dfs(index):
    global ans
    if index == N:
        if blst.count(1) >= K - 1:
            tmp = a[0]
            anstmp = 0
            for i in range(1, N):
                if blst[i - 1] == 0:
                    if tmp < a[i]:
                        tmp = a[i]
                    continue
                if tmp >= a[i]:
                    anstmp += tmp - a[i] + 1
                    tmp += 1
                else:
                    tmp = a[i]
            ans = min(ans, anstmp)
        return
    for i in range(2):
        blst[index - 1] = i
        dfs(index + 1)
    return

N, K = map(int, input().split())
a = list(map(int, input().split()))
blst = [0] * (N - 1)
ans = float('inf')
dfs(1)

print(ans)

# Buggy version:
N,K = map(int,input().split())
a_list = list(map(int,input().split()))

# 建物iをミられる高さにするかいなかのbit探索

ans = pow(10,100000000000)

for byte in range(pow(2,N-1)):
    height = a_list[0]
    cost = 0
    if bin(byte).count("1")>=K-1:
        for i in range(N-1):
            if not (byte & 1<<i)==0: # i番目を見る場合
                cost += max(0, height + 1 - a_list[i+1])
                height = max(height + 1, a_list[i+1])
        if cost < ans:
            ans = cost
print(ans)

# Fixed version:

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

# 建物iをミられる高さにするかいなかのbit探索

ans = 100010001000100010001000

for byte in range(pow(2,N-1)):
    height = a_list[0]
    cost = 0
    if bin(byte).count("1")>=K-1:
        for i in range(N-1):
            if not (byte & 1<<i)==0: # i番目を見る場合
                cost += max(0, height + 1 - a_list[i+1])
                height = max(height + 1, a_list[i+1])
            else:
                height = max(height,a_list[i+1])
        ans = min(ans,cost)
print(ans)

# Buggy version:
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations
    #from itertools import accumulate, product, permutations
    from math import floor, ceil
    import copy

    #mod = 1000000007

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    res = 10**17
    for i in range(2**n):
        total, m, cnt = 0, 0, 0
        for j in range(n):
            if(i>>j) & 1:
                cnt += 1
                total += max(m-a[j]+1, 0)
                m = max(m, a[j])
            else:
                m = max(m, a[j])
        if cnt == k:
            res = min(res, total)
    print(res)

if __name__ == '__main__':
    main()

# Fixed version:
def main():
    import sys
    #input = sys.stdin.readline
    sys.setrecursionlimit(10000000)
    from collections import Counter, deque
    #from collections import defaultdict
    from itertools import combinations
    #from itertools import accumulate, product, permutations
    from math import floor, ceil
    import copy

    #mod = 1000000007

    n,k = map(int, input().split())
    a = list(map(int, input().split()))
    res = 10**17
    for i in range(2**n):
        total, m, cnt = 0, 0, 0
        for j in range(n):
            if(i>>j) & 1:
                cnt += 1
                total += max(m-a[j]+1, 0)
                m = max(m+1, a[j])
            else:
                m = max(m, a[j])
        if cnt == k:
            res = min(res, total)
    print(res)

if __name__ == '__main__':
    main()

# Buggy version:
import ipdb
from itertools import combinations
N,K = map(int,input().split())
A = list(map(int,input().split()))
cmin = 10**12
for x in combinations(range(1,N),K-1):
    C = A[:]
    B = [0]
    B += x
    B = sorted(B)
    cnt = 0
    for i in range(1,K):
        j0 = B[i-1]
        j1 = B[i]
        h = 0
        for j in range(j0,j1):
            h = max(h,C[j])
        if C[j1]<=h:
            cnt += h+1-C[j1]
            C[j1] = h+1
    cmin = min(cmin,cnt)
print(cmin)

# Fixed version:
from itertools import combinations
N,K = map(int,input().split())
A = list(map(int,input().split()))
cmin = 10**12
for x in combinations(range(1,N),K-1):
    C = A[:]
    B = [0]
    B += x
    B = sorted(B)
    cnt = 0
    for i in range(1,K):
        j0 = B[i-1]
        j1 = B[i]
        h = 0
        for j in range(j0,j1):
            h = max(h,C[j])
        if C[j1]<=h:
            cnt += h+1-C[j1]
            C[j1] = h+1
    cmin = min(cmin,cnt)
print(cmin)