S = gets.chomp
K = gets.to_i

count = 0
count_base = 0

ss = S.split("")

if ss.uniq.size == 1 then
	count = ss.size * K / 2
else
	head = 0
	tail = 0

	if ss[0] == ss[-1] then
		head = ss.index{|x| x != ss[0]}
		tail = ss.size - ss.rindex{|x| x != ss[0]} - 1
	end

	temp = nil
	temp_count = 0

	(head ... ss.size - tail).each{ |i|
		if temp == ss[i] then
			temp_count += 1
		else
			count_base += temp_count / 2
			temp_count = 1
		end
		temp = ss[i]
	}

	if temp_count != 0 then
		count_base += temp_count / 2
	end

	count = count_base * K
	count = (head + tail) / 2 * (K - 1)
	
	count += head / 2
	count += tail / 2
end

puts count