S=gets.chomp
K=gets.to_i

if S.length == 1
    puts K/2
    exit 0
elsif S.length == 2
    if S[0] == S[1]
        puts K
    else
        puts 0
    end
    exit 0
end

pc = nil
cnt = 0
a = []
S.each_char do |c|
    if pc == c
        cnt += 1
    else
        a << [pc,cnt]
        pc = c
        cnt = 1
    end
end
a << [pc,cnt]
a.shift

if a[0][0] == a[-1][0]
    ret = 0
    a[1..-2].each do |c,cnt|
        ret += cnt/2 * K
    end
    ret += (a[0][1]+a[-1][1])/2*(K-1)
    ret += a[0][1]/2
    ret += a[-1][1]/2
    puts ret
else
    ret = 0
    a.each do |c,cnt|
        ret += cnt/2 * K
    end
    puts ret
end
