S=gets.chomp
K=gets.chomp.to_i

if S.size == 1
  puts K/2
else
  have_to_change_count = S.scan(/(.)\1+?/).size
  if S[0] == S[-1] &&
    S.scan(/^#{S[0]}+/).first&.size.odd? &&
    S.reverse.scan(/^#{S[-1]}+/).first.size.odd?

    puts ((have_to_change_count + 1) * K - 1)
  else
    puts (have_to_change_count * K)
  end
end