Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = 1
            count += 1
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[0] == S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] == S[-2]:
            S[-1] = 1
            count += 1
        elif S[0] == S[1] and S[-1] == S[-2]:
            S[0] = 1
            count += 1
        elif S[0] != S[1] and S[-1] != S[-2]:
            S[0] = 1
            count += 1
        
        
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = 1
                count += 1
        print(K*count)

Fixed submission:
from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)

Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if S[i] == S[i+1]:
            S[i+1] = 1
            count += 1
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[0] == S[1]:
            S[0] = 1
            count += 1
        for i in range(len(S)-1):
            if S[i] == S[i+1]:
                S[i+1] = 1
                count += 1
        print(K*count)

Fixed submission:
from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)

Buggy python submission (verdict: Runtime Error):
S = input()
K = int(input())
count = 0
if S[0] != S[-1]:
    flag = False
    for i in range(len(S)-1):
        if not flag and S[i] == S[i+1]:
            count += 1
            flag = True
        if flag:
            flag = False
    print(K*count)
else:
    if len(S) == 2:
        print(K)
    else:
        if S[-1] == S[-2]:
            S = S[:-1]+'X'
            count = 1
        flag = False
        for i in range(len(S)-1):
            if not flag and S[i] == S[i+1]:
                count += 1
                flag = True
            if flag:
                flag = False
        print(K*count)

Fixed submission:
from collections import Counter
s = input()
N = len(s)
K = int(input())
if s[0] != s[-1]:
    count = 1
    ans = 0
    now = s[0]
    for i in range(1,len(s)):
        if now == s[i]:
            count += 1
        else:
            now = s[i]
            ans += count//2
            count = 1
    if count != 1:
        ans += count//2
    print(ans*K)
elif len(Counter(s).values()) == 1:
    print(K*len(s)//2)
else:
    ans = 0
    fcount = 1
    bcount = 1
    now = s[0]
    start = N
    for i in range(1,N):
        if s[i] == now:
            fcount += 1
        else:
            start = i
            break
    now = s[-1]
    end= 0
    for i in range(N-2,-1,-1):
        if s[i] == now:
            bcount += 1
        else:
            end = i
            break   
    ans = fcount//2 + bcount//2 + ((fcount+bcount)//2)*(K-1)
    now = s[start]
    count = 1
    mcount = 0
    for i in range(start+1,end+1):
        if s[i] == now:
            count += 1
        else:
            mcount += count//2
            count = 1
            now = s[i]
    if count != 1:
        mcount += count//2
    print(ans + mcount*K)