s = list(str(eval(input())))

k = int(eval(input()))



n = len(s)



cur = s[0]

L = []

cnt = 0

for i in range(n):

    if cur == s[i]:

        cnt += 1

    else:

        L.append(cnt)

        cur = s[i]

        cnt = 1

else:

    L.append(cnt)

#print(L)



if s[0] != s[-1]:

    C = 0

    for i in range(len(L)):

        if L[i] >= 2:

            C += L[i]//2

    ans = C*k

else:

    if len(L) >= 2:

        C = 0

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

            if L[i] >= 2:

                C += L[i]//2

        ans = C*k

        ans += L[0]//2+L[-1]//2+((L[0]+L[-1])//2)*(k-1)

    else:

        ans = (L[0]*k)//2

print(ans)

s = str(eval(input()))

k = int(eval(input()))



n = len(s)



s_ = list(s)

if len(set(s_)) == 1:

    print(((n*k)//2))

    exit()



cur = s[0]

X = []

cnt = 0

for i in range(n):

    if s[i] != cur:

        X.append(cnt)

        cur = s[i]

        cnt = 1

    else:

        cnt += 1

else:

    X.append(cnt)



#print(X)

ans = 0

if s[0] != s[-1]:

    for x in X:

        ans += k*(x//2)

else:

    for i, x in enumerate(X):

        if i == 0 or i == len(X)-1:

            continue

        else:

            ans += k*(x//2)

    ans += X[0]//2

    ans += X[-1]//2

    ans += (k-1)*((X[0]+X[-1])//2)

print(ans)

s = list(str(eval(input())))
k = int(eval(input()))
n = len(s)
cur = s[0]
L = []
cnt = 0
for i in range(n):
    if cur == s[i]:
        cnt += 1
    else:
        L.append(cnt)
        cur = s[i]
        cnt = 1
else:
    L.append(cnt)
# print(L)
if s[0] != s[-1]:
    C = 0
    for i in range(len(L)):
        if L[i] >= 2:
            C += L[i] // 2
    ans = C * k
else:
    if len(L) >= 2:
        C = 0
        for i in range(1, len(L) - 1):
            if L[i] >= 2:
                C += L[i] // 2
        ans = C * k
        ans += L[0] // 2 + L[-1] // 2 + ((L[0] + L[-1]) // 2) * (k - 1)
    else:
        ans = (L[0] * k) // 2
print(ans)

s = str(eval(input()))
k = int(eval(input()))
n = len(s)
s_ = list(s)
if len(set(s_)) == 1:
    print(((n * k) // 2))
    exit()
cur = s[0]
X = []
cnt = 0
for i in range(n):
    if s[i] != cur:
        X.append(cnt)
        cur = s[i]
        cnt = 1
    else:
        cnt += 1
else:
    X.append(cnt)
# print(X)
ans = 0
if s[0] != s[-1]:
    for x in X:
        ans += k * (x // 2)
else:
    for i, x in enumerate(X):
        if i == 0 or i == len(X) - 1:
            continue
        else:
            ans += k * (x // 2)
    ans += X[0] // 2
    ans += X[-1] // 2
    ans += (k - 1) * ((X[0] + X[-1]) // 2)
print(ans)

-s = list(str(eval(input()))) +s = str(eval(input())) +s_ = list(s) +if len(set(s_)) == 1: +    print(((n * k) // 2)) +    exit() -L = [] +X = [] -    if cur == s[i]: -        cnt += 1 -    else: -        L.append(cnt) +    if s[i] != cur: +        X.append(cnt) +    else: +        cnt += 1 -    L.append(cnt) -# print(L) +    X.append(cnt) +# print(X) +ans = 0 -    C = 0 -    for i in range(len(L)): -        if L[i] >= 2: -            C += L[i] // 2 -    ans = C * k +    for x in X: +        ans += k * (x // 2) -    if len(L) >= 2: -        C = 0 -        for i in range(1, len(L) - 1): -            if L[i] >= 2: -                C += L[i] // 2 -        ans = C * k -        ans += L[0] // 2 + L[-1] // 2 + ((L[0] + L[-1]) // 2) * (k - 1) -    else: -        ans = (L[0] * k) // 2 +    for i, x in enumerate(X): +        if i == 0 or i == len(X) - 1: +            continue +        else: +            ans += k * (x // 2) +    ans += X[0] // 2 +    ans += X[-1] // 2 +    ans += (k - 1) * ((X[0] + X[-1]) // 2)