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

grouped = (S.split("").inject([]) do |memo, item|
  memo << [] if memo.last == nil
  if memo.last.last == nil || memo.last.last == item then
    memo.last << item
    next memo
  end

  memo << [item]
  next memo
end)

same = false
if grouped.size != 1 && grouped.first[0] == grouped.last[0] then
  same = true if grouped.last.size == 1
  grouped.first.concat(grouped.last)
  grouped.pop
end

total = 0

grouped.each do |list|
  total += (list.size / 2).floor
end

# if grouped.first[0] == grouped.last[0] && grouped.last.size == 1 then
#   total += 1
#   same = true
# end

answer = total * K
answer -= 1 if same

puts answer