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 aaa
implicit none

integer(16) :: k, i, j, l, left, right, mid, res
character(105) :: 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