Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())
N = len(S)

C0 = []
cnt = 1
for i in range(1,N):
    if S[i]==S[i-1]:
        cnt+=1
        if i==N-1:
            C0.append(cnt)
    else:
        C0.append(cnt)
        cnt = 1
out0 = 0
for m in C0:
    out0 += m//2

if S[0]!=S[-1]:
    out = out0*K
else:
    X = (C0[0] + C0[-1])//2
    Y = C0[0]//2 + C0[-1]//2
    out = (out0-Y)*K
    out += X*(K-1)
    out += Y
print(out)


Fixed submission:
S = list(input())
K = int(input())

N = len(S)
first = S[0]
last = S[-1]
now = 1
ing = 0
a = []
cnt = 1
flag = 0
if N > 1:
    while now < N:
        if S[now] == S[now-1]:
            ing = 1
            cnt += 1
            now += 1
            continue
        else:
            if ing ==1:
                a.append(cnt)
                cnt = 1
                now +=1
                ing = 0
            else:
                now +=1
                cnt = 1
                ing = 0
    if ing ==1:
        a.append(cnt)

    output = 0
    for j in range(len(a)):
        output += a[j]//2

    if first==last:
        if S[0]==S[1]:
            if S[-1]==S[-2]:
                X =a[0]
                Y =a[-1]
            else:
                X = a[0]
                Y = 1
        else:
            if S[-1]==S[-2]:
                X = 1
                Y =a[-1]
            else:
                X = 1
                Y = 1

        if (X==1)and(Y==1):
            output += 1
            flag = 1
        elif X==1:
            if Y%2==1:
                output+=1
                flag = 1
        elif Y==1:
            if X%2==1:
                output+=1
                flag = 1
        else:
            if (X%2==1)and(Y%2==1):
                output += 1
                flag = 1

    output = output * K - flag
    if (len(a)==1)and(a[0]==N):
        output = (K*N)//2
elif N == 1:
    output = K//2

print(output)

Buggy python submission (verdict: Runtime Error):
S = list(input())
K = int(input())

N = len(S)
first = S[0]
last = S[-1]
now = 1
ing = 0
a = []
cnt = 1
flag = 0
while now < N:
    if S[now] == S[now-1]:
        ing = 1
        cnt += 1
        now += 1
        continue
    else:
        if ing ==1:
            a.append(cnt)
            cnt = 1
            now +=1
            ing = 0
        else:
            now +=1
            cnt = 1
            ing = 0
if ing ==1:
    a.append(cnt)

output = 0
for j in range(len(a)):
    output += a[j]//2

if first==last:
    if S[0]==S[1]:
        if S[-1]==S[-2]:
            X =a[0]
            Y =a[-1]
        else:
            X = a[0]
            Y = 1
    else:
        if S[-1]==S[-2]:
            X = 1
            Y =a[-1]
        else:
            X = 1
            Y = 1

    if (X==1)and(Y==1):
        output += 1
        flag = 1
    elif X==1:
        if Y%2==1:
            output+=1
            flag = 1
    elif Y==1:
        if X%2==1:
            output+=1
            flag = 1
    else:
        if (X%2==1)and(Y%2==1):
            output += 1
            flag = 1

output = output * K - flag

print(output)

Fixed submission:
S = list(input())
K = int(input())

N = len(S)
first = S[0]
last = S[-1]
now = 1
ing = 0
a = []
cnt = 1
flag = 0
if N > 1:
    while now < N:
        if S[now] == S[now-1]:
            ing = 1
            cnt += 1
            now += 1
            continue
        else:
            if ing ==1:
                a.append(cnt)
                cnt = 1
                now +=1
                ing = 0
            else:
                now +=1
                cnt = 1
                ing = 0
    if ing ==1:
        a.append(cnt)

    output = 0
    for j in range(len(a)):
        output += a[j]//2

    if first==last:
        if S[0]==S[1]:
            if S[-1]==S[-2]:
                X =a[0]
                Y =a[-1]
            else:
                X = a[0]
                Y = 1
        else:
            if S[-1]==S[-2]:
                X = 1
                Y =a[-1]
            else:
                X = 1
                Y = 1

        if (X==1)and(Y==1):
            output += 1
            flag = 1
        elif X==1:
            if Y%2==1:
                output+=1
                flag = 1
        elif Y==1:
            if X%2==1:
                output+=1
                flag = 1
        else:
            if (X%2==1)and(Y%2==1):
                output += 1
                flag = 1

    output = output * K - flag
    if (len(a)==1)and(a[0]==N):
        output = (K*N)//2
elif N == 1:
    output = K//2

print(output)