fun main(args: Array<String>) {
    val s = readLine()!!
    val n = s.length.toLong()
    val k = readLine()!!.toLong()

    if (s.toSet().size == 1) {
        println(n * k / 2)
        return
    }

    val count = mutableListOf<Pair<Char,Long>>()
    var si = 0
    var first = 0L
    while (si < n) {
        var to = si + 1
        while (to < n && s[to] == s[si]) to++
        val diff = to - si
        count.add(Pair(s[si], diff.toLong()))
        si = to

        first += diff / 2
    }

    if (count.first().first == count.last().first) {
        val firstCount = count.first().second
        val lastCount = count.last().second
        val second = first - firstCount / 2 - lastCount / 2 + (firstCount + lastCount) / 2
        println(first + second * (k - 1))
    } else {
        println(first * k)
    }
}