# frozen_string_literal: true

s = gets.chomp.chars
k = gets.to_i
n = s.size
a = []
count = 1
(0..(n - 2)).each do |i|
  if s[i] == s[i + 1]
    count += 1
  else
    a.push count
    count = 1
  end
end
a.push count
sum = 0
if a.size == 1
  sum = a[0] * k / 2
elsif s[0] != s[-1]
  sum = a.map { |e| e / 2 }.reduce(&:+) * k
else
  t = a.pop
  h = a.shift
  sum = a.map { |e| e / 2 }.reduce(&:+) * k
  sum += t / 2 + h / 2 + (t + h) / 2 * (k - 1)
end
puts sum
