program aaa
implicit none

integer(8) :: k, i, j, l, left, right, mid, res
character(100) :: s
character(1) :: a

read*, s
read*, k

left=0
right=0
mid=0
res=0

l=len_trim(s)
s=trim(s)

if(s(1:1)==s(l:l)) then
a=s(1:1)
 do i=1,l
  if(a==s(i:i)) then
  left=left+1
  else
  exit
  end if
 end do
 do i=l,1,-1
  if(a==s(i:i)) then
  right=right+1
  else
  exit
  end if
 end do
 do i=1,left
 s(i:i)='#'
 end do
 do i=l,(l-right+1),-1
 s(i:i)='#'
 end do
end if

if(2*l==left+right) then
write(*,'(i0)') k*l/2
return
end if


do i=1,l
 if (s(i:i)/='#') then
  do j=i+1,l
   if(s(i:i)==s(j:j)) then
   mid=mid+1
   s(j:j)='#'
    if(j/=l) then
     if(s(i:i)/=s(j+1:j+1)) then
     mid=mid+1
     res=res+mid/2
     mid=0
     end if
    end if
   else
   exit
   end if
  end do
 end if
end do

res=res*k+((left+right)/2)*(k-1)+left/2+right/2
write(*,'(i0)') res

end program