program AGC039A
    implicit none
    character(100)::S
    integer(8)::K,i,ans,cont,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=ans+(K-1)*(cont+cont_first)/2
        ans=ans+cont_first/2+cont/2
    else
        ans=ans+cont/2
        ans=ans*K
    end if
    print'(i0)',ans
end program AGC039A