import java.io.PrintWriter
val pw = PrintWriter(System.out)

fun main(args : Array<String>) {
    func()
    pw.flush()
}

fun func() {
    val s = next()
    val k = nextLong()

    // aaaaaaaa... => a_a_a_a_ or a_a_a_a
    if (s.length == 1 || s.toList().distinct().size == 1) {
        println((s.length * k) / 2L)
        return
    }

    var changed = false
    var count = 0L

    for (i in 1 until s.length) {
        if (changed) {
            changed = false
            continue
        }
        if (s[i] == s[i-1]) {
            count++
            changed = true
        }
    }

    if (k == 1L) {
        println(count)
        return
    }

    // not continuous
    // 1) first and last letters are not the same
    // abbbc => ab_bc|ab_bc|... == count * k
    if (s.first() != s.last()) {
        println(count * k)
        return
    }

    val firstLetter = s.first()
    val lastLetter = s.last()
    val startCount = s.takeWhile { it == firstLetter }.length
    val endCount = s.takeLastWhile { it == lastLetter }.length

    // 2) even numbers of the same letter at the end
    // abbbaa => ab_ba_|ab_ba_|...
    // == count * k
    if (endCount % 2 == 0) {
        println(count * k)
        return
    }

    // continuous
    // 3) even numbers of the same letter at the beginning && odd numbers of the same letter at the end
    // aabbbaaa => a_b_ba_a|_ab_ba_a|_ab_ba_a|...
    // == count * k
    if (startCount % 2 == 0 && endCount % 2 == 1) {
        println(count * k)
        return
    }

    // 4) odd numbers of the same letter at the beginning && odd numbers of the same letter at the end
    // abbbaaa => ab_ba_a|_b_ba_a|_b_ba_a|...
    // (count + 1) * k - 1
    if (startCount % 2 == 1 && endCount % 2 == 1) {
        println((count + 1) * k - 1)
        return
    }
}

fun next() = readLine()!!
fun nextLong() = next().toLong()

fun println(value : Any) {
    pw.println(value)
}
