S = gets.chomp
K = gets.to_i
ans = 0
if K == 1 || S[0] != S[-1]
    curr = nil
    streak = 1
    S.chars.each do |s|
        if curr == s
            streak += 1
        else
            ans += streak / 2
            streak = 1
        end
        curr = s
    end
    ans += streak / 2
    ans = ans * K 
elsif S.chars.uniq.size == 1
    ans = S.length * K / 2
else
    curr = nil
    streak = 1
    streaks = []
    S.chars.each do |s|
        if curr == s
            streak += 1
        else
            streaks << streak
            streak = 1
        end
        curr = s
    end
    streaks << streak
    streaks[1..-2].each{|st| ans += (st / 2) *  K}
    ans += streaks[0] / 2
    ans += streaks[-1] / 2
    ans += (streaks[0] + streaks[-1]) / 2 * (K - 1)
end
p ans