S = gets.chomp.split("")
K = gets.to_i

#文字列の連続回数を配列にまとめるcontinous_count_arrayを作る
continous_count_array = Array.new()
continous_count = 1

S.each.with_index do |s, i|
  next_word = S[i + 1]
  if next_word == s
    continous_count += 1
  else
    continous_count_array << continous_count
    continous_count = 1
  end
end
p continous_count_array

#もし「qqqq」のように1文字だったら
if continous_count_array.size == 1
  countinous_count = continous_count_array[0] * K
  answer1 = countinous_count / 2
  judge = 1
end
#ここまで


if continous_count_array.size >= 2
continous_first = continous_count_array.shift
continous_first_per = continous_first / 2
coutinous_last = continous_count_array.pop
coutinous_last_per = coutinous_last / 2
answer = continous_first_per + coutinous_last_per

if !continous_count_array.empty?
#最初の文字と最後の文字が一致してた時の処理
if S[0] == S[-1]
  continous_LF = coutinous_last + continous_first
  countinous_LF_per = continous_LF / 2
  answer += countinous_LF_per * (K - 1)

    countinous_count_per = Array.new()
  continous_count_array.each do |c|
    countinous_count_per << (c / 2) * K
  end
  normal = countinous_count_per.inject(:+)
  answer += normal
  
else
  answer = continous_first_per * K + coutinous_last_per * K
  countinous_count_per = Array.new()
  continous_count_array.each do |c|
    countinous_count_per << (c / 2) * K
  end
  normal = countinous_count_per.inject(:+)
  answer += normal
end
end  
end

if judge == 1
  p answer1
else
  p answer
end