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

end=0
if K!=1:
    S_old = S
    i=0
    while S_old[-1] == S_old[i]:
        S= S + S[i]
        i+=1
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break
    if renzoku >1:
        ans+=renzoku//2
    i+=1
if K== 1:
    print(ans)
else:
    S=S_old
    ans = ans*(K-1)
    i=0
    while S[i] == S[0]:
        i+=1
    S=S[i:]
    i=0
    while i<len(S)-1:
        renzoku=1
        x=i
        while S[x] == S[i+1]:
            renzoku+=1
            if i+2 < len(S):
                i=i+1
            else:
                break
        if renzoku >1:
            ans+=renzoku//2
        i+=1
    print(ans)

Fixed submission:
S=input()
K=int(input())
flg=False
for s in S:
    if S[0] != s:
        flg=True
        break
if not flg:
    print(len(S)*K//2)
    exit()
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break

    if renzoku >1:
        ans+=renzoku//2
    i+=1

if S[0] == S[-1]:
    s=S[0]
    top=0
    bottom=0
    i=0
    while S[i] == s:
        top+=1
        i+=1
    i=-1
    while S[i] == s:
        bottom+=1
        i-=1
    if (top%2) + (bottom%2) == 2:
        print((ans+1)*K-1)
        exit()
    
print(ans*K)

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

end=0
if K!=1:
    S_old = S
    i=-1
    while S_old[0] == S_old[i]:
        S= S + S[0]
        i-=1
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break
    if renzoku >1:
        ans+=renzoku//2
    i+=1
if K== 1:
    pass
else:
    S=S_old
    ans = ans*(K-1)
    i=0
    while S[i] == S[0]:
        i+=1
    S=S[i:]
    i=0
    while i<len(S)-1:
        renzoku=1
        x=i
        while S[x] == S[i+1]:
            renzoku+=1
            if i+2 < len(S):
                i=i+1
            else:
                break
        if renzoku >1:
            ans+=renzoku//2
        i+=1
print(ans)

Fixed submission:
S=input()
K=int(input())
flg=False
for s in S:
    if S[0] != s:
        flg=True
        break
if not flg:
    print(len(S)*K//2)
    exit()
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break

    if renzoku >1:
        ans+=renzoku//2
    i+=1

if S[0] == S[-1]:
    s=S[0]
    top=0
    bottom=0
    i=0
    while S[i] == s:
        top+=1
        i+=1
    i=-1
    while S[i] == s:
        bottom+=1
        i-=1
    if (top%2) + (bottom%2) == 2:
        print((ans+1)*K-1)
        exit()
    
print(ans*K)

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

ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        i=i+1
        renzoku+=1
    if renzoku >1:
        ans+=renzoku//2
    i+=1
    
print(ans*K)

Fixed submission:
S=input()
K=int(input())
flg=False
for s in S:
    if S[0] != s:
        flg=True
        break
if not flg:
    print(len(S)*K//2)
    exit()
ans = 0
i=0
while i<len(S)-1:
    renzoku=1
    x=i
    while S[x] == S[i+1]:
        renzoku+=1
        if i+2 < len(S):
            i=i+1
        else:
            break

    if renzoku >1:
        ans+=renzoku//2
    i+=1

if S[0] == S[-1]:
    s=S[0]
    top=0
    bottom=0
    i=0
    while S[i] == s:
        top+=1
        i+=1
    i=-1
    while S[i] == s:
        bottom+=1
        i-=1
    if (top%2) + (bottom%2) == 2:
        print((ans+1)*K-1)
        exit()
    
print(ans*K)