program connection_and_disconnection
  implicit none
  character(100) :: s
  integer :: n, i, x(100) = 0, y(100) = 0
  integer(8) :: k, a1 = 0_8, a2 = 0_8
  read(*,*) s
  read(*,*) k
  n = len_trim(s)
  if (n == 1) then
    write(*,'(i0)') k/2_8
    stop
  end if
  do i = 1, n
    x(i) = ichar(s(i:i))-96
    y(i) = x(i)
  end do
  do i = 2, n
    if (x(i) == x(i-1)) then
      x(i) = 100
      a1 = a1+k
    end if
  end do
  if (x(n) == x(1)) a1 = a1+k-1_8
  do i = n-1, 1, -1
    if (y(i) == y(i+1)) then
      y(i) = 100
      a2 = a2+k
    end if
  end do
  if (y(1) == y(n)) a2 = a2+k-1_8
  write(*,'(i0)') min(a1,a2)
end program connection_and_disconnection