S=gets.chomp
K=gets.to_i

if S.chars.uniq.length == 1
    puts S.length*K/2
    exit 0
end

a = S.chars.chunk_while {|c1,c2| c1 == c2 }.map(&:count)
ret = a.inject(0) {|sum,cnt| sum += cnt/2 * K }
if S[0] == S[-1]
    x=a[0]
    y=a[-1]
    ret -= x/2+y/2-(x+y)/2
end
puts ret