program AGC39_A
  character(100)::S
  integer(8)::i,J=1,K,L(100)=1,M(100)=1,N
  read(*,*)S
  read(*,*)K
  N=len_trim(S)
  do i=1,N-1
     if(S(i:i)==S(i+1:i+1)) then
        L(J)=L(J)+1
     else
        J=J+1
     end if
  end do
  if(S(1:1)/=S(N:N)) then
     L=L/2
     write(*,*)sum(L)*K
  else
     M=L
     if(J/=1) then
        M(J)=M(J)+M(1)
        M(1)=0
        M=M/2
        L=L/2
        write(*,*)L(1)+sum(M)*K-M(J)+L(J)
     else
        M=M/2
        write(*,*)sum(M)*K
     end if
  end if
end program AGC39_A