# coding: utf-8

# Your code here!

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

S=list(eval(input()))



l=[]

count=1

for i in range(N-1):

    if S[i]!=S[i+1]:

        l.append(count)

        count=1

    else:

        count+=1

l.append(count)



flag=False

ans=0

idx=0

time=0

while idx<len(l):

    if time<K:

        time+=1

        ans+=(l[idx]+l[idx+1] if idx!=(len(l)-1) else l[idx])

        idx+=1

    else:

        flag=True

        ans+=(l[idx]-1)

        

    idx+=1



if flag:

    print(ans)

else:

    print((ans-1))





# coding: utf-8

# Your code here!

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



S=list(eval(input()))



clu=[]

temp=0

for i in range(1,len(S)):

    if S[i]==S[i-1]:

        temp+=1

    else:

        clu.append(temp)

        temp=0

clu.append(temp)

 

ans=sum(clu)



base=S[0]



zone=False

for i in range(1,len(S)):

    if zone:

        if S[i]==base:

            K-=1

            ans+=2

            zone=False

        else:

            S[i]=base

    else:

        if S[i]!=base and K>0:

            zone=True

            S[i]=base



if zone:

    ans+=1



print(ans)
# coding: utf-8
# Your code here!
N, K = list(map(int, input().split()))
S = list(eval(input()))
l = []
count = 1
for i in range(N - 1):
    if S[i] != S[i + 1]:
        l.append(count)
        count = 1
    else:
        count += 1
l.append(count)
flag = False
ans = 0
idx = 0
time = 0
while idx < len(l):
    if time < K:
        time += 1
        ans += l[idx] + l[idx + 1] if idx != (len(l) - 1) else l[idx]
        idx += 1
    else:
        flag = True
        ans += l[idx] - 1
    idx += 1
if flag:
    print(ans)
else:
    print((ans - 1))

# coding: utf-8
# Your code here!
N, K = list(map(int, input().split()))
S = list(eval(input()))
clu = []
temp = 0
for i in range(1, len(S)):
    if S[i] == S[i - 1]:
        temp += 1
    else:
        clu.append(temp)
        temp = 0
clu.append(temp)
ans = sum(clu)
base = S[0]
zone = False
for i in range(1, len(S)):
    if zone:
        if S[i] == base:
            K -= 1
            ans += 2
            zone = False
        else:
            S[i] = base
    else:
        if S[i] != base and K > 0:
            zone = True
            S[i] = base
if zone:
    ans += 1
print(ans)

-l = [] -count = 1 -for i in range(N - 1): -    if S[i] != S[i + 1]: -        l.append(count) -        count = 1 +clu = [] +temp = 0 +for i in range(1, len(S)): +    if S[i] == S[i - 1]: +        temp += 1 -        count += 1 -l.append(count) -flag = False -ans = 0 -idx = 0 -time = 0 -while idx < len(l): -    if time < K: -        time += 1 -        ans += l[idx] + l[idx + 1] if idx != (len(l) - 1) else l[idx] -        idx += 1 +        clu.append(temp) +        temp = 0 +clu.append(temp) +ans = sum(clu) +base = S[0] +zone = False +for i in range(1, len(S)): +    if zone: +        if S[i] == base: +            K -= 1 +            ans += 2 +            zone = False +        else: +            S[i] = base -        flag = True -        ans += l[idx] - 1 -    idx += 1 -if flag: -    print(ans) -else: -    print((ans - 1)) +        if S[i] != base and K > 0: +            zone = True +            S[i] = base +if zone: +    ans += 1 +print(ans)