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