s = gets.chomp
k = gets.to_i

rl = s.chars.slice_when { |i, j| i != j }.map { |e| e.size }
count = k * rl.map { |e| e / 2 }.inject(:+)

if rl.size == 1
  puts s.size * k / 2
elsif s[0] == s[-1]
  puts count - (k - 1) * (rl[0] / 2 + rl[-1] / 2 - (rl[0] + rl[-1]) / 2)
else
  puts count
end