import sys

input = sys.stdin.readline

from collections import *



S = input()[:-1]

K = int(eval(input()))



if S==S[0]*len(S):

    print((len(S)*K//2))

    exit()

    

l = []

cnt = 1



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

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

        l.append((S[i], cnt))

        cnt = 1

    else:

        cnt += 1

    

l.append((S[-1], cnt))



if K==1 or l[0][0]!=l[-1][0]:

    ans = 0

    

    for _, c in l:

        ans += c//2*K

else:

    ans = (l[0][1]+l[-1][1])//2*(K-1)

    ans += l[0][1]//2

    ans += l[-1][1]//2

    

    for _, c in l[1:-1]:

        ans += c//2*K



print(ans)
S = eval(input())

K = int(eval(input()))



if S==S[0]*len(S):

    print((len(S)*K//2))

    exit()

    

rle = []

cnt = 1



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

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

        rle.append((S[i], cnt))

        cnt = 1

    else:

        cnt += 1



rle.append((S[-1], cnt))

#print(rle)



if rle[0][0]==rle[-1][0]:

    ans = rle[0][1]//2+rle[-1][1]//2

    

    for i in range(1, len(rle)-1):

        ans += rle[i][1]//2*K

    

    ans += (rle[0][1]+rle[-1][1])//2*(K-1)

else:

    ans = 0

    

    for i in range(len(rle)):

        ans += rle[i][1]//2*K

    

print(ans)
import sys

input = sys.stdin.readline
from collections import *

S = input()[:-1]
K = int(eval(input()))
if S == S[0] * len(S):
    print((len(S) * K // 2))
    exit()
l = []
cnt = 1
for i in range(len(S) - 1):
    if S[i] != S[i + 1]:
        l.append((S[i], cnt))
        cnt = 1
    else:
        cnt += 1
l.append((S[-1], cnt))
if K == 1 or l[0][0] != l[-1][0]:
    ans = 0
    for _, c in l:
        ans += c // 2 * K
else:
    ans = (l[0][1] + l[-1][1]) // 2 * (K - 1)
    ans += l[0][1] // 2
    ans += l[-1][1] // 2
    for _, c in l[1:-1]:
        ans += c // 2 * K
print(ans)

S = eval(input())
K = int(eval(input()))
if S == S[0] * len(S):
    print((len(S) * K // 2))
    exit()
rle = []
cnt = 1
for i in range(len(S) - 1):
    if S[i] != S[i + 1]:
        rle.append((S[i], cnt))
        cnt = 1
    else:
        cnt += 1
rle.append((S[-1], cnt))
# print(rle)
if rle[0][0] == rle[-1][0]:
    ans = rle[0][1] // 2 + rle[-1][1] // 2
    for i in range(1, len(rle) - 1):
        ans += rle[i][1] // 2 * K
    ans += (rle[0][1] + rle[-1][1]) // 2 * (K - 1)
else:
    ans = 0
    for i in range(len(rle)):
        ans += rle[i][1] // 2 * K
print(ans)

-import sys - -input = sys.stdin.readline -from collections import * - -S = input()[:-1] +S = eval(input()) -l = [] +rle = [] -        l.append((S[i], cnt)) +        rle.append((S[i], cnt)) -l.append((S[-1], cnt)) -if K == 1 or l[0][0] != l[-1][0]: +rle.append((S[-1], cnt)) +# print(rle) +if rle[0][0] == rle[-1][0]: +    ans = rle[0][1] // 2 + rle[-1][1] // 2 +    for i in range(1, len(rle) - 1): +        ans += rle[i][1] // 2 * K +    ans += (rle[0][1] + rle[-1][1]) // 2 * (K - 1) +else: -    for _, c in l: -        ans += c // 2 * K -else: -    ans = (l[0][1] + l[-1][1]) // 2 * (K - 1) -    ans += l[0][1] // 2 -    ans += l[-1][1] // 2 -    for _, c in l[1:-1]: -        ans += c // 2 * K +    for i in range(len(rle)): +        ans += rle[i][1] // 2 * K