S, K = gets.chomp, gets.to_i
a = S.scan(/((.)\2*)/).map{|_1,| _1.size }
isCyc = (1 < a.size and S[0] == S[-1])
hosei = a[0]/2 + a[-1]/2
a[-1] += a.shift if isCyc
hosei -= a[-1]/2
p K*a.inject(0){|acc,_| acc+_/2 } + (isCyc ? hosei : 0)
