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

count = 0
l = len(S)
check = True
i = 0
while i < l - 1:
    if S[i] == S[i + 1]:
        count += 1
        i += 2
        if i + 1 == l:
            check = False
    else:
        i += 1

ff = 1
while S[ff] == S[0] and ff < l:
    ff += 1
ll = 1
while S[-ll] == S[-1] and ll < l:
    ll += 1

if S[0] == S[-1]:
    if not check:
        print(count * K)
    else:
        print(count + (K - 1) * (ff + ll) // 2)
else:
    print(count * K)


Fixed submission:
S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l * K // 2)
    exit()

i = ff
c = 1
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 1
    i += 1
count += c // 2

# print(count, ff, ll)

print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)

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

count = 0
l = len(S)
check = True
i = 0
while i < l - 1:
    if S[i] == S[i + 1]:
        count += 1
        i += 2
        if i + 1 == l:
            check = False
    else:
        i += 1

ff = 1
while S[ff] == S[0] and ff < l:
    ff += 1
ll = 1
while S[-ll] == S[-1] and ll < l:
    ll += 1

if S[0] == S[-1]:
    if not check:
        print(count * K)
    else:
        print(count + (K - 1) * (ff + ll) // 2)
else:
    print(count * K)


Fixed submission:
S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l * K // 2)
    exit()

i = ff
c = 1
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 1
    i += 1
count += c // 2

# print(count, ff, ll)

print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)

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

count = 0
l = len(S)
check = True
i = 0
while i < l - 1:
    if S[i] == S[i + 1]:
        count += 1
        i += 2
        if i + 1 == l:
            check = False
    else:
        i += 1

ff = 1
while S[ff] == S[0]:
    ff += 1
ll = 1
while S[-ll] == S[-1]:
    ll += 1

if S[0] == S[-1]:
    if not check:
        print(count * K)
    else:
        print(count + (K - 1) * (ff + ll) // 2)
else:
    print(count * K)


Fixed submission:
S = input()
K = int(input())
SS = S[::-1]

count = 0
l = len(S)
check = True
ff = 1
while ff < l:
    if S[ff] == S[0]:
        ff += 1
    else:
        break
ll = 1
while ll < l:
    if SS[ll] == SS[0]:
        ll += 1
    else:
        break

if S[0] != SS[0]:
    ff = 0
    ll = 0

if ff == l:
    print(l * K // 2)
    exit()

i = ff
c = 1
while i < l - ll - 1:
    if S[i] == S[i + 1]:
        c += 1
    else:
        count += c // 2
        c = 1
    i += 1
count += c // 2

# print(count, ff, ll)

print(K * count + (K - 1) * ((ff + ll) // 2) + ff // 2 + ll // 2)