fun main(args: Array<String>) {
    agc039a()
}

fun agc039a() {
    val s = readLine()!!
    val k = readLine()!!.toLong()

    if (s.length == 1) return println(k / 2L)

    var lastChar: Char? = null
    val charCount = mutableListOf<Long>()
    for (i in s.indices) {
        if (lastChar != null && s[i] == lastChar) {
            val last = charCount[charCount.lastIndex]
            charCount.removeAt(charCount.lastIndex)
            charCount.add(last + 1L)
        } else {
            charCount.add(1L)
            lastChar = s[i]
        }
    }

    val inside = charCount.map { it / 2L }.sum()

    for (i in s.indices) {
        if (lastChar != null && s[i] == lastChar) {
            val last = charCount[charCount.lastIndex]
            charCount.removeAt(charCount.lastIndex)
            charCount.add(last + 1L)
        } else {
            charCount.add(1L)
            lastChar = s[i]
        }
    }

    val between = charCount.map { it / 2L }.sum() - inside

    for (i in s.indices) {
        if (lastChar != null && s[i] == lastChar) {
            val last = charCount[charCount.lastIndex]
            charCount.removeAt(charCount.lastIndex)
            charCount.add(last + 1L)
        } else {
            charCount.add(1L)
            lastChar = s[i]
        }
    }

    val between2 = charCount.map { it / 2L }.sum() - between - inside

    val answer = inside + if (between == between2) {
        between * (k - 1L)
    } else {
        (between * Math.ceil((k - 1L).toDouble() / 2) + between2 * Math.floor((k - 1).toDouble() / 2)).toLong()
    }

    println(answer)
}
