S = gets.chomp
K = gets.to_i

def count(s)
  res = 0
  s.scan(/(.)\1+/){res += $&.size/2}
  res
end

single = count(S)
double = count(S+S)
triple = count(S+S+S)

if double - single == triple - double
  p single + (K - 1) * (double - single)
else
  a = (double - single) * 2 - 1
  p a*K/2
end
