Problem:
Score : 300 points

Problem Statement

Given is a string S. Let T be the concatenation of K copies of S.
We can repeatedly perform the following operation: choose a character in T and replace it with a different character.
Find the minimum number of operations required to satisfy the following condition: any two adjacent characters in T are different.

Constraints

1 \leq |S| \leq 100

S consists of lowercase English letters.

1 \leq K \leq 10^9

K is an integer.

Input

Input is given from Standard Input in the following format:

S
K

Output

Print the minimum number of operations required.

Sample Input 1

issii
2

Sample Output 1

4

T is issiiissii. For example, we can rewrite it into ispiqisyhi, and now any two adjacent characters are different.

Sample Input 2

qq
81

Sample Output 2

81

Sample Input 3

cooooooooonteeeeeeeeeest
999993333

Sample Output 3

8999939997

Solution in Fortran (gfortran v4.8.4) (judge verdict: Wrong Answer)

program AGC039A
    implicit none
    character(100)::S
    integer(8)::K,i,ans,cont,ans_add,cont_first,flg=0
    read*,S,K
    ans=0
    cont=1
    do i=2,len_trim(S)
        if(S(i:i)==S(i-1:i-1))then
            cont=cont+1
        else
            if(flg==0)then
                cont_first=cont
                flg=1
            end if
            ans=ans+cont/2
            cont=1
        end if
    end do

    if(S(1:1)==S(len_trim(S):len_trim(S)))then
        ans=ans-cont_first/2
        ans=ans*K
        ans_add=(cont+cont_first)/2
        ans=ans+(K-1)*ans_add
        ans=ans+cont_first/2+cont/2
    else
        ans=ans+cont/2
        ans=ans*K
    end if
    print'(i0)',ans
end program AGC039A